Creating a scheduled task in Windows takes five minutes. Then the time comes and nothing happens, or the program starts but you never see its window. Almost always it's one of a few defaults. The defaults below come from New-ScheduledTaskSettingsSet, and the result codes from Microsoft's Task Scheduler constants.
The four gates
A task has to get through four gates before anything visible happens:
1. Trigger -> 0x41303: the task has not yet run
2. Conditions -> on battery: doesn't start (default)
3. Security options -> "whether user is logged on or not": no window
4. Action -> 0x80070002: file not found, 0x1: the program's exit code
The Status and Last Run Result columns and the History tab tell you which gate stopped it. If History is empty, click Enable All Tasks History in the Actions pane. The same events are in Event Viewer under Applications and Services Logs > Microsoft > Windows > TaskScheduler > Operational.
No window: the security option
Run whether user is logged on or not runs the task in session 0, not on your desktop. A GUI program still starts but never shows up, and the task sits at 0x41301 (currently running). The default for an already running task is Do not start a new instance, so the next scheduled run is skipped too.
- Programs you need to see: Run only when user is logged on, the default for Create Basic Task.
- Scripts that need no window: whether logged on or not, which also stops the console window flashing. You have to save the password. Do not store password uses an S4U logon, which per Microsoft's docs has no access to the network or encrypted files, so skip it for backups to a NAS.
Only fails on a laptop: the power conditions
Two defaults on the Conditions tab:
- Start the task only if the computer is on AC power: on
- Stop if the computer switches to battery power: on
Clear both for tasks that should run on battery.
The other defaults
New-ScheduledTaskSettingsSet with no arguments shows them:
DisallowStartIfOnBatteries : True
StopIfGoingOnBatteries : True
StartWhenAvailable : False <- runs missed while the PC was off are skipped
WakeToRun : False
ExecutionTimeLimit : PT72H <- stopped after 3 days
MultipleInstances : IgnoreNew
The 3-day limit bites anything meant to stay open, like trading apps or servers. And an empty Start in (optional) makes the task run from C:\Windows\System32, so a program that loads its config from a relative path fails only when scheduled. For a batch file, cd /d "%~dp0" on the first line fixes it.
Reading Last Run Result
| Code | Meaning |
|---|---|
0x0 |
Success (exit code 0) |
0x41301 |
The task is currently running |
0x41303 |
The task has not yet run |
0x80070002 |
The system cannot find the file specified |
0x800710E0 |
The operator or administrator has refused the request |
0x1, 0x2 ... |
Usually the program's own exit code |
0x41303 isn't a fault. Plenty of built-in tasks only run in specific situations: on the PC I checked, 76 of about 200 tasks showed it. For a batch file that ends with 0x1, append >> log.txt 2>&1 to its commands to see which one failed.
Creating it from the command line
schtasks has no Start in option, so if you need one, use PowerShell and fix the defaults while you're at it:
$action = New-ScheduledTaskAction -Execute "C:\Tools\report.exe" -WorkingDirectory "C:\Tools"
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -ExecutionTimeLimit ([TimeSpan]::Zero)
Register-ScheduledTask -TaskName "Morning report" -Action $action -Trigger $trigger -Settings $settings
For quick one-liners schtasks is fine. Paths with spaces need an inner pair of single quotes:
schtasks /create /tn "Report" /tr "'C:\Program Files\Report\report.exe' --quiet" /sc daily /st 07:00
schtasks /run /tn "Report"
schtasks /query /tn "Report" /v /fo list
The full guide, with diagrams and the Create Basic Task walkthrough:
Which default has bitten you?
Top comments (0)