Scheduled Task Cleanup Script
This PowerShell script simplifies the process of cleaning up old scheduled tasks on a Windows server. Regular maintenance of scheduled tasks is critical for ensuring optimal server performance and reducing clutter. Administrators utilizing ServerEngine can benefit from this script by keeping their scheduled tasks organized and efficient. The script allows users to specify a date threshold, deleting tasks that have not run since that date.
param ( [int]$daysThreshold = 30 ) # Calculate the cutoff date $cutoffDate = (Get-Date).AddDays(-$daysThreshold) # Get scheduled tasks and filter by last run time $oldTasks = Get-ScheduledTask | Where-Object { $_.LastRunTime -lt $cutoffDate -and $_.LastRunTime -ne $null } foreach ($task in $oldTasks) { # Remove the old scheduled task Unregister-ScheduledTask -TaskName $task.TaskName -Confirm:$false Write-Output "Removed old scheduled task: $($task.TaskName)" } Write-Output "Scheduled task cleanup completed. Tasks older than $daysThreshold days have been removed."