Automating recurring administrative tasks in Windows environments traditionally required navigating the graphical Task Scheduler console (taskschd.msc) or executing complex command-line syntax using schtasks.exe. While Task Scheduler handles process execution, it does not manage PowerShell script blocks or output serialization natively, requiring administrators to write cumbersome wrapper scripts to capture errors.
Windows PowerShell provides the Scheduled Job feature set (via the PSScheduledJob module). Scheduled jobs marry the enterprise triggering infrastructure of Windows Task Scheduler with the native object pipeline of PowerShell background jobs. Scheduled jobs run asynchronously in the background, survive user logoffs, and store their execution outputs for later retrieval.
Understanding PowerShell Scheduled Jobs Architecture
Unlike standard background jobs created with Start-Job (which terminate immediately when your current PowerShell session closes), scheduled jobs:
- Are registered directly within the Windows Task Scheduler hierarchy under
MicrosoftWindowsPowerShellScheduledJobs. - Can be triggered on calendar intervals, at system boot, or on user logon.
- Persist execution history, stdout, and error records on disk inside
$env:LocalAppDataMicrosoftWindowsPowerShellScheduledJobs.
Creating a Scheduled Job with Register-ScheduledJob
To define and register a recurring background job:
- Open PowerShell as an Administrator.
- Create a trigger object using
New-JobTrigger. For example, to trigger daily at 3:00 AM:$trigger = New-JobTrigger -Daily -At "3:00 AM" - Register the scheduled job with a script block:
Register-ScheduledJob -Name "PurgeTempFiles" -Trigger $trigger -ScriptBlock { Get-ChildItem -Path "$env:TEMP*" -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Force -Recurse [PSCustomObject]@{ Timestamp = Get-Date Status = "Temp cleanup completed successfully" } }
Configuring Advanced Job Options (New-ScheduledJobOption)
To customize wake-on-LAN conditions, network requirements, or elevated security privileges:
$options = New-ScheduledJobOption -RunElevated -RequireNetwork -WakeToRun
Set-ScheduledJobOption -Name "PurgeTempFiles" -ScheduledJobOption $options
The -RunElevated parameter guarantees that the script block executes with full administrative privileges under the Windows Task Scheduler.
Querying and Inspecting Scheduled Job Results
After a scheduled job triggers, you can query its execution history and deserialize its return objects using standard job cmdlets:
- List all registered scheduled jobs on the system:
Get-ScheduledJob - Inspect historical execution runs:
Get-Job -Name "PurgeTempFiles" - Retrieve the serialized output objects from the most recent run:
TheReceive-Job -Name "PurgeTempFiles" -Keep-Keepswitch preserves the cached output in storage so it can be queried multiple times.
Updating or Removing Scheduled Jobs
To modify the trigger of an existing job:
$newTrigger = New-JobTrigger -Weekly -DaysOfWeek Sunday -At "1:00 AM"
Set-JobTrigger -Name "PurgeTempFiles" -Trigger $newTrigger
To permanently delete a scheduled job and unregister it from Windows Task Scheduler:
Unregister-ScheduledJob -Name "PurgeTempFiles"
Using PowerShell Scheduled Jobs enables reproducible, infrastructure-as-code automation across Windows workstations and servers with complete error transparency.