Task Scheduler is the Windows equivalent of cron: it runs scripts and programs on a schedule, at startup, at logon, or when an event happens. On a Windows VPS it powers backups, cleanups, reports, application restarts and automation scripts. Creating tasks with PowerShell makes them repeatable and easy to document.

Linux equivalent: crontab.

Create a daily task

$action  = New-ScheduledTaskAction -Execute "powershell.exe" `
           -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\cleanup.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 3:00
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Hours 2) `
            -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 5)

Register-ScheduledTask -TaskName "Nightly cleanup" -TaskPath "\Ops\" `
  -Action $action -Trigger $trigger -Settings $settings `
  -User "SYSTEM" -RunLevel Highest
  • -StartWhenAvailable: run a missed task as soon as possible (for example after a reboot).
  • -ExecutionTimeLimit: stop runaway jobs.
  • Task folders (-TaskPath "\Ops\") keep your tasks separate from Windows’ own.

Triggers

New-ScheduledTaskTrigger -AtStartup
New-ScheduledTaskTrigger -AtLogOn -User "trader"
New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 4:00
New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(5) -RepetitionInterval (New-TimeSpan -Minutes 15)

The last one repeats every 15 minutes indefinitely (or add -RepetitionDuration).

Start an application at boot (for example a trading terminal or a bot) with -AtStartup for services, or -AtLogOn for apps that need a desktop session.

Which account should run it?

Account Use when
SYSTEM System maintenance; needs no password; full local rights
NETWORK SERVICE / LOCAL SERVICE Limited rights; network access as the computer
A dedicated user Needs that user’s profile, mapped settings, or least privilege
“Run only when user is logged on” GUI apps that must appear on the desktop

With a dedicated user and “run whether logged on or not”, supply the password at registration (-User "svc-backup" -Password "…"), and give the account “Log on as a batch job” rights (granted automatically for admins).

Manage tasks

Get-ScheduledTask -TaskPath "\Ops\"
Get-ScheduledTaskInfo -TaskName "Nightly cleanup" -TaskPath "\Ops\"   # LastRunTime, LastTaskResult, NextRunTime
Start-ScheduledTask -TaskName "Nightly cleanup" -TaskPath "\Ops\"
Disable-ScheduledTask -TaskName "Nightly cleanup" -TaskPath "\Ops\"
Unregister-ScheduledTask -TaskName "Nightly cleanup" -TaskPath "\Ops\" -Confirm:$false

Export and import tasks as XML, for documentation or another server:

Export-ScheduledTask -TaskName "Nightly cleanup" -TaskPath "\Ops\" | Out-File C:\Scripts\nightly-cleanup.xml
Register-ScheduledTask -Xml (Get-Content C:\Scripts\nightly-cleanup.xml -Raw) -TaskName "Nightly cleanup"

Logging and history

  • Write your own log from the script: Start-Transcript -Path C:\Scripts\logs\cleanup-$(Get-Date -f yyyyMMdd).log -Append.
  • LastTaskResult 0 means success. Other codes: 0x1 (the script returned an error), 0x41301 (still running), 0x41303 (never run).
  • Enable task history in Task Scheduler (Action → Enable All Tasks History), or check Microsoft-Windows-TaskScheduler/Operational in Event Viewer. See Windows Event Viewer errors.

Troubleshooting

Symptom Fix
Works manually, not scheduled Different account/profile; use full paths; set “Start in” folder
Script never starts Execution policy: pass -ExecutionPolicy Bypass; check the account’s rights
Network shares unavailable Mapped drives don’t exist in tasks; use UNC paths (\\server\share)
GUI app invisible It runs in session 0; use “Run only when user is logged on”
Task runs twice / overlaps Settings → “Do not start a new instance” (-MultipleInstances IgnoreNew)

Useful scheduled jobs for a VPS

  • Backups and off-site sync. See Windows Server Backup.
  • Log and temp cleanup. See Windows disk cleanup.
  • Health checks that alert you (disk space, service status).
  • Restarting apps that leak memory, at quiet hours.

Quick reference

Task Cmdlet
Action New-ScheduledTaskAction -Execute … -Argument …
Trigger New-ScheduledTaskTrigger -Daily -At 3:00
Register Register-ScheduledTask -TaskName … -User SYSTEM
Status Get-ScheduledTaskInfo
Run now Start-ScheduledTask
Remove Unregister-ScheduledTask

Related: PowerShell basics and browser automation on a Windows VPS.

  • Windows Server
  • PowerShell
  • Automation