PowerShell Remove-Job
last modified February 15, 2025
In this article, we will cover the Remove-Job
cmdlet in
PowerShell. This cmdlet deletes background jobs created with Start-Job.
Job basics
A PowerShell job runs commands in the background without interacting with
the current session. Jobs are useful for long-running tasks. Each job has
a unique ID and state (Running, Completed, Failed). The Remove-Job
cmdlet cleans up finished jobs to free resources.
Basic Remove-Job usage
The simplest way to use Remove-Job
is with a job object. First
create a job with Start-Job, then remove it. This frees system resources
used by the job. Always verify job completion before removal.
$job = Start-Job -ScriptBlock { Get-Process } Remove-Job -Job $job
This creates a background job to list processes, then removes it. The job object is stored in $job variable for reference. Verify job state with Get-Job before removal.
Remove job by ID
Jobs can be removed by their ID instead of object reference. First get the job ID from Get-Job output. Then pass the ID to Remove-Job. This is useful when working with multiple jobs.
Start-Job -ScriptBlock { Start-Sleep -Seconds 10 } $jobId = (Get-Job).Id Remove-Job -Id $jobId
This starts a sleep job, gets its ID, then removes it. The job ID is captured from Get-Job output. Use this method when you need to reference jobs by ID.
Remove multiple jobs
Multiple jobs can be removed at once by passing an array of job objects. This is efficient for cleaning up many completed jobs. Use Get-Job to retrieve all jobs first. Then pipe or pass them to Remove-Job.
Start-Job -ScriptBlock { 1..3 | ForEach-Object { Start-Sleep -Seconds 1 } } Start-Job -ScriptBlock { Get-ChildItem } Get-Job | Remove-Job
This creates two jobs, then removes all jobs with Get-Job pipeline. The command removes both running and completed jobs. Be cautious as this removes all jobs without filtering.
Remove completed jobs only
You can filter jobs by state before removal. The -State parameter filters jobs by their execution state. This example removes only completed jobs. Running or failed jobs are preserved.
Start-Job -ScriptBlock { Get-Service } Get-Job -State Completed | Remove-Job
This starts a service listing job, then removes only completed jobs. The -State parameter ensures running jobs aren't accidentally removed. Always check job states before bulk removal.
Force remove running jobs
Running jobs can be forcibly removed with the -Force parameter. This stops the job before removal. Use cautiously as it terminates active processes. Data loss may occur with forced removal.
$job = Start-Job -ScriptBlock { while($true) { Start-Sleep -Seconds 1 } } Remove-Job -Job $job -Force
This creates an infinite loop job, then forces its removal. The -Force parameter stops the running job. Only use this when necessary to avoid unexpected termination.
Remove jobs by name
Jobs can be named when created for easier identification. The -Name parameter allows removing jobs by their assigned name. This helps manage jobs in complex scripts.
Start-Job -Name "DiskCheck" -ScriptBlock { Get-Disk } Remove-Job -Name "DiskCheck"
This creates a named disk checking job, then removes it by name. Named jobs are easier to manage than IDs or objects. Use descriptive names for better job tracking.
Remove jobs with confirmation
The -Confirm parameter adds a safety prompt before job removal. This prevents accidental deletion of important jobs. The user must confirm each removal action.
Start-Job -ScriptBlock { Get-EventLog -LogName System } Remove-Job -Id 1 -Confirm
This starts a system log job, then removes it with confirmation. The user must type 'Y' to confirm removal. Use this for critical jobs where accidental removal could cause issues.
Remove all jobs silently
The -Force and -Confirm parameters can be combined for bulk operations. This example removes all jobs without confirmation prompts. Use with caution as it's irreversible.
Start-Job -ScriptBlock { Get-HotFix } Start-Job -ScriptBlock { Get-NetAdapter } Get-Job | Remove-Job -Force -Confirm:$false
This creates two jobs, then removes all jobs silently. The -Confirm:$false suppresses prompts while -Force stops running jobs. Reserve this for scripts where no user interaction is desired.
Source
In this article, we have covered the Remove-Job cmdlet in PowerShell.
Author
List all PowerShell tutorials.