cheat-sheets

Windows Scheduled Tasks

Task Scheduler runs programs and scripts at set times or on events, like cron on Linux (Cron). Manage it with the PowerShell ScheduledTasks module, schtasks.exe or taskschd.msc. Commands run in PowerShell (PowerShell Cheat-Sheet) and most need an elevated prompt.

Documentation: ScheduledTasks module, schtasks

PowerShell #

COMMANDDESCRIPTION
Get-ScheduledTaskList tasks
Get-ScheduledTask -TaskName "<NAME>" | Get-ScheduledTaskInfoLast run time and result
Get-ScheduledTask -TaskPath "\Microsoft\Windows\*"Tasks under a folder
Start-ScheduledTask -TaskName "<NAME>"Run now
Stop-ScheduledTask -TaskName "<NAME>"Stop a running task
Disable-ScheduledTask -TaskName "<NAME>"Disable
Enable-ScheduledTask -TaskName "<NAME>"Enable
Unregister-ScheduledTask -TaskName "<NAME>" -Confirm:$falseDelete (destructive)
Export-ScheduledTask -TaskName "<NAME>"Output the task as XML
Register-ScheduledTask -Xml (Get-Content task.xml -Raw) -TaskName "<NAME>"Import from XML

Create a Task #

powershell
$action  = New-ScheduledTaskAction -Execute "powershell.exe" `
  -Argument "-NoProfile -File C:\Scripts\backup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 3am
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName "Nightly backup" -Action $action `
  -Trigger $trigger -Principal $principal
CMDLETDESCRIPTION
New-ScheduledTaskActionWhat to run (-Execute, -Argument, -WorkingDirectory)
New-ScheduledTaskTriggerWhen: -Once, -Daily, -Weekly, -AtStartup, -AtLogOn
New-ScheduledTaskPrincipalWho it runs as and the run level
New-ScheduledTaskSettingsSetOptions such as -StartWhenAvailable, -RestartCount, -ExecutionTimeLimit
Set-ScheduledTaskChange an existing task
Register-ScheduledTaskCreate it

Run as SYSTEM or a service account for jobs that must run when nobody is signed in. For a named user, pass -User and -Password to Register-ScheduledTask instead of a principal.

schtasks.exe #

COMMANDDESCRIPTION
schtasks /queryList tasks
schtasks /query /tn "<NAME>" /v /fo listVerbose details for one task
schtasks /create /tn "<NAME>" /tr "C:\Scripts\run.cmd" /sc daily /st 03:00 /ru SYSTEMCreate a daily task
schtasks /create /tn "<NAME>" /tr "C:\Scripts\run.cmd" /sc onstart /ru SYSTEMRun at startup
schtasks /run /tn "<NAME>"Run now
schtasks /end /tn "<NAME>"Stop
schtasks /change /tn "<NAME>" /disableDisable (/enable to reverse)
schtasks /delete /tn "<NAME>" /fDelete (destructive)

Add /s <COMPUTER> to any command to target a remote machine.

Troubleshooting #

  • A result of 0 (or 0x0) from Get-ScheduledTaskInfo is success. Other codes are in LastTaskResult.
  • Scripts run with a minimal environment and the task’s working directory, so use full paths.
  • Task history is off by default. Enable it in taskschd.msc (Action, Enable All Tasks History), then read the Microsoft-Windows-TaskScheduler/Operational log, see Windows Troubleshooting.
  • “Run whether user is logged on or not” needs the account to have “Log on as a batch job” rights.

Not Verified #

Parameter names and the task history menu path were written from memory. Check Get-Help <CMDLET> -Full on your Windows version.

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.