Task Scheduler runs programs and scripts for you: at startup, at a set time, or when a user signs in. It is the Windows answer to cron and the usual way to automate backups, clean-ups and restarts of your own tools.
Create a task with the graphical tool
- Press Windows + R, type
taskschd.mscand press Enter. - Click Create Task (not "Create Basic Task", which hides useful options).
- General: give it a name. Choose Run whether user is logged on or not for scripts that need no window, and tick Run with highest privileges if the script needs admin rights.
- Triggers → New: for example Daily at 03:00, or At startup.
- Actions → New: Start a program. For a PowerShell script, program
powershell.exeand arguments-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\cleanup.ps1". - Settings: tick If the task fails, restart every 5 minutes if it should retry.
- Click OK and enter the account password when asked.
Create the same task in PowerShell
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\cleanup.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -TaskName "Nightly cleanup" -Action $action -Trigger $trigger -User "SYSTEM" -RunLevel HighestRun it once now to test, and see the result:
Start-ScheduledTask -TaskName "Nightly cleanup"
Get-ScheduledTaskInfo -TaskName "Nightly cleanup" | Select-Object LastRunTime, LastTaskResultA LastTaskResult of 0 means success.
Warning: Tasks set to run whether the user is logged on or not, or running as SYSTEM, have no visible desktop. Programs with a window, such as MetaTrader or a browser, will not appear. For those, use an At log on trigger and Run only when user is logged on, as shown in Auto-start MetaTrader after a reboot.
Good uses
- Copy important folders to a backup location every night (backup guide).
- Delete log files older than 30 days.
- Restart a service that occasionally hangs.
Troubleshooting
- Task says it ran but nothing happened: the script probably uses relative paths. Use full paths, or set Start in on the action.
- Result 0x1: the script itself returned an error. Run the same command by hand in PowerShell to see the message.
- Task never starts: check the History tab (enable it with Enable All Tasks History in the right panel).
