PowerShell: A Scheduled Task Template

The Version Worth Copying

Most scheduled task snippets you find online pass a username and password in plaintext on the command line. That password ends up in your shell history, in any transcript of the screen, and in the task definition where a logged-in user can read it back.

The template below avoids storing a password at all, which is the correct default.

$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' `
          -Argument '-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\test.ps1'

$trigger = New-ScheduledTaskTrigger -Daily -At 3am

$settings = New-ScheduledTaskSettingsSet `
          -StartWhenAvailable `
          -WakeToRun `
          -ExecutionTimeLimit (New-TimeSpan -Hours 1)

Register-ScheduledTask -TaskName 'NightlyScript' `
    -Action $action -Trigger $trigger -Settings $settings `
    -User 'SYSTEM' -RunLevel Highest

-User 'SYSTEM' means the task runs as the machine account with no stored credential. It is the right choice for anything that does not need a user profile — backups, log rotation, cleanup, service restarts.

A photorealistic shot of a laptop showing the Windows Task Scheduler console with a task list, a PowerShell window beside it, office desk with papers, daylight, realistic photography, no legible text.

If It Needs A User Account

Some scripts genuinely need a user's mapped drives, profile or credentials. Then use SYSTEM-adjacent options in this order of preference:

A Group Managed Service Account (gMSA) if you have a domain. The password rotates automatically and you never handle it.

-User with the logged-on user and no password, registered from that user's own session — valid for tasks that run only when that user is logged in:

Register-ScheduledTask -TaskName 'MyTask' -Action $action -Trigger $trigger -User $env:USERNAME

A stored credential, prompted rather than typed into the script:

$cred = Get-Credential
Register-ScheduledTask -TaskName 'MyTask' -Action $action -Trigger $trigger `
    -User $cred.UserName -Password $cred.GetNetworkCredential().Password

The password is still stored in the task definition — Windows has to be able to start the task without you — but at least it is not in your script, your history, or a screenshot. Restrict who can read the task with an ACL if it matters.

The Settings That Actually Matter

Defaults for Register-ScheduledTask are not defaults for schtasks /create, and the differences are what make a task silently not run.

-StartWhenAvailable — if the machine was off at the trigger time, run as soon as it comes back. Without it, a laptop that was closed at 3am simply skips the run.

-WakeToRun — wake the machine from sleep. Only works if the machine supports it and hibernate/sleep settings allow it; it is a hint, not a guarantee.

-ExecutionTimeLimit — kills a hung script. The default is 3 days, which is not a useful safety net. An hour is generous for most jobs.

-RunLevel Highest — run elevated. Required for anything touching disks, services or system paths. Without it the script runs unelevated and fails quietly on the first privileged operation.

Checking It Later

Get-ScheduledTask -TaskName 'NightlyScript' | Get-ScheduledTaskInfo
Start-ScheduledTask -TaskName 'NightlyScript'
Unregister-ScheduledTask -TaskName 'NightlyScript' -Confirm:$false

Get-ScheduledTaskInfo reports LastRunTime and LastTaskResult. That result code is where the answer is when a task "ran but did nothing" — 0x0 is success, 0x41301 means it is still running, and anything else is a real failure worth looking up.

A photorealistic close-up of a Task Scheduler window showing a task's last run result and history, a notebook with a result code written down, warm desk light, realistic photography, no legible text.

A photorealistic shot of a laptop showing a scheduled task's history list with run results, a printed script on the desk beside it, warm lamp light, realistic photography, no legible text.

Three Traps

Smart quotes. If you copy a snippet from a blog post (including the older version of this one), the quotes may be typographic " and " rather than ASCII ". PowerShell reads those as part of the string and the command fails with a parse error that points at the wrong place. Retype the quotes.

Relative paths break. A scheduled task starts in C:\Windows\System32. Any relative path in your script resolves there, not where the script lives. Use absolute paths, or derive them from $PSScriptRoot.

-ExecutionPolicy Bypass is doing less than you think. It bypasses the policy for that process, but a script downloaded from the internet also carries a Zone.Identifier mark, and RemoteSigned will still refuse it. Unblock-File is usually the actual fix — see the execution policy article.

Don't trust the download button. Trust the hash.

If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.


Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz