EMZETT.
Login

Windows Task Scheduler: starting programs automatically

In short: The Task Scheduler runs programs and scripts at fixed times or on events, such as backups, cleanup jobs or scripts at sign-in.

In more detail: On Windows the Task Scheduler is the counterpart of cron and systemd timers. It starts tasks time-driven or event-driven, even without a signed-in user.

In Depth

Creating a task

Start → “Task Scheduler” → Create Basic Task or Create Task (more options). You define: trigger (daily, weekly, at startup, at logon, on event, when idle), action (start program, arguments, working folder) and conditions (only on AC power, only with network). Important: option “Run whether user is logged on or not” for background jobs and “Run with highest privileges” only when necessary.

On the command line

$a = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File C:\Scripts\backup.ps1'
$t = New-ScheduledTaskTrigger -Daily -At 3am
Register-ScheduledTask -TaskName 'Backup' -Action $a -Trigger $t
Get-ScheduledTask | Where State -eq Ready
Start-ScheduledTask -TaskName 'Backup'

Classic: schtasks /create /tn Backup /tr "C:\Scripts\backup.bat" /sc daily /st 03:00.

Typical tasks and pitfalls

  • Backups, deleting temp files, generating reports, sync scripts.
  • Use absolute paths and set the working folder, otherwise the script does not find files.
  • Password/account: if the task runs as a user, the password must be right (error 0x8007052e/0x1).
  • Execution policy for PowerShell (-ExecutionPolicy Bypass only deliberately).
  • Check the History tab (enable it) and the Event Viewer to see why something failed; “Last run result” 0x0 = success.
  • Laptops on battery often do not start tasks; check conditions.
  • Missed tasks: “Run task as soon as possible after a scheduled start is missed”.

Security

Tasks with high privileges are a popular persistence mechanism of malware. Occasionally check unknown tasks (Sysinternals Autoruns), protect script folders from write access by normal users and store no plain-text passwords in scripts. On Linux this corresponds to cron/systemd timers, on macOS launchd.

See also: PowerShell