Background Jobs vs Scheduled Tasks
A PowerShell background job, started with Start-Job, runs a script block asynchronously in a separate process on the same machine for the duration of the current session, and its results are retrieved later with Receive-Job; a Windows Scheduled Task, by contrast, is a persistent, OS-level definition that survives reboots and can trigger a script at a specific time, on a recurring schedule, or in response to an event, independent of any PowerShell session being open. Choosing between them depends on whether the work needs to happen right now within a running script (a job) or needs to run reliably in the future even if no one is logged in (a scheduled task).
Cricket analogy: Start-Job is like bringing on a substitute fielder mid-innings who works alongside the current match, while a Scheduled Task is like a fixture already locked into next season's calendar regardless of who's currently playing.
Working with Background Jobs
Start-Job -ScriptBlock { ... } returns a job object immediately while the script block executes in a child process; Get-Job lists all jobs and their State (Running, Completed, Failed), Receive-Job retrieves the output once it's ready, and -Wait blocks until the job finishes if you need synchronous behavior. Jobs should be cleaned up with Remove-Job once their output has been received, since PowerShell keeps completed job objects and their output in memory until explicitly removed, which can accumulate in long-running sessions or scripts that launch many jobs in a loop.
Cricket analogy: Get-Job checking State is like glancing at the scoreboard to see if an innings is 'In Progress' or 'Completed' before deciding whether to check the final total.
Creating and Managing Scheduled Tasks
The ScheduledTasks module provides New-ScheduledTaskTrigger to define when a task runs (for example -Daily -At '3:00AM'), New-ScheduledTaskAction to define what it runs (typically powershell.exe with -Argument pointing at a script), and Register-ScheduledTask to combine a trigger, action, and principal into a named task visible in Task Scheduler. Once registered, Get-ScheduledTask, Start-ScheduledTask, Disable-ScheduledTask, and Unregister-ScheduledTask let you inspect, manually trigger, pause, or delete the task entirely, and Get-ScheduledTaskInfo reveals the LastRunTime, LastTaskResult, and NextRunTime for monitoring whether a task is actually succeeding on its schedule.
Cricket analogy: New-ScheduledTaskTrigger -Daily -At '3:00AM' is like a franchise pre-committing to a fixed training-session time every day of the tour, locked in regardless of who's managing that week.
# Background job example
$job = Start-Job -ScriptBlock {
Get-ChildItem -Path 'C:\Data' -Recurse -Filter '*.csv'
}
Get-Job -Id $job.Id
$results = Receive-Job -Id $job.Id -Wait
Remove-Job -Id $job.Id
# Register a daily scheduled task that runs a cleanup script at 3 AM
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NoProfile -File "C:\Scripts\Cleanup.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At '3:00AM'
Register-ScheduledTask -TaskName 'NightlyCleanup' -Action $action -Trigger $trigger -RunLevel Highest
# Check whether the task last succeeded
Get-ScheduledTaskInfo -TaskName 'NightlyCleanup' | Select-Object LastRunTime, LastTaskResult, NextRunTimeLastTaskResult of 0 means the scheduled task's last run exited successfully; nonzero values are Win32 error codes that indicate a failure, so monitoring scripts should check LastTaskResult -ne 0 to detect silently failing automation.
- Background jobs (Start-Job) run asynchronously within the current machine and session; scheduled tasks persist across reboots and logins.
- Get-Job, Receive-Job, and Remove-Job form the lifecycle for creating, retrieving, and cleaning up background jobs.
- Jobs accumulate in memory until explicitly removed with Remove-Job, so long-running scripts should clean them up.
- New-ScheduledTaskTrigger defines when a task runs; New-ScheduledTaskAction defines what command it executes.
- Register-ScheduledTask combines a trigger and action into a named task visible in Task Scheduler.
- Get-ScheduledTaskInfo reports LastRunTime, LastTaskResult, and NextRunTime for monitoring task health.
- Disable-ScheduledTask pauses a task without deleting it; Unregister-ScheduledTask removes it permanently.
Practice what you learned
1. What is the key difference between a PowerShell background job and a Windows Scheduled Task?
2. Which cmdlet retrieves the output of a completed background job?
3. Why should Remove-Job be called after retrieving a job's output?
4. Which two cmdlets are combined via Register-ScheduledTask to define when and what a scheduled task runs?
5. What does a LastTaskResult of 0 from Get-ScheduledTaskInfo indicate?
Was this page helpful?
You May Also Like
Processes and Services
Learn how to inspect, start, stop, and control Windows processes and services using PowerShell's process and service management cmdlets.
Remoting with PowerShell
Learn how PowerShell Remoting uses WinRM to run commands on remote machines, including interactive sessions, one-off invocations, and persistent connections.
Working with Files and Folders
Learn how PowerShell treats the file system as a navigable provider, and how to create, read, write, copy, move, and delete files and folders with dedicated cmdlets.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics