Automate Scheduled Tasks Management with PowerShell
In this post, we will provide a PowerShell script that automates the management of scheduled tasks on Windows systems. Scheduled tasks are essential for automating repetitive jobs and ensuring that critical operations run at specified times. This script allows administrators to easily retrieve, create, and delete scheduled tasks, streamlining the management of automation jobs across multiple servers.
Here is the PowerShell script for managing scheduled tasks:
# Define task details $taskName = "DailyReport" $taskDescription = "Generates a daily report at 7 AM." $scriptPath = "C:\Scripts\DailyReport.ps1" # Replace with the path to your script # Create a new scheduled task $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File `"$scriptPath`"" $trigger = New-ScheduledTaskTrigger -At 7am -Daily $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable # Register the scheduled task Register-ScheduledTask -TaskName $taskName -Description $taskDescription -Action $action -Trigger $trigger -Settings $settings Write-Host "Scheduled task '$taskName' created successfully to run at 7 AM."