PowerShell Logo Small

Stop-Job



This is the built-in help made by Microsoft for the command 'Stop-Job', in PowerShell version 5 - as retrieved from Windows version 'Microsoft Windows Server 2012 R2 Standard' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Stops a Windows PowerShell background job.

SYNTAX


Stop-Job [-Id] <Int32[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Stop-Job [-Filter] <Hashtable> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Stop-Job [-InstanceId] <Guid[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Stop-Job [-Job] <Job[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Stop-Job [-Name] <String[]> [-PassThru] [-Confirm] [-WhatIf] [<CommonParameters>]
Stop-Job [-State] {NotStarted | Running | Completed | Failed | Stopped | Blocked | Suspended | Disconnected | Suspending | Stopping | AtBreakpoint} [-PassThru] [-Confirm]
[-WhatIf] [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


The Stop-Job cmdlet stops Windows PowerShell background jobs that are in progress. You can use this cmdlet to stop all jobs or stop selected jobs based on their name, ID,
instance ID, or state, or by passing a job object to Stop-Job.


You can use Stop-Job to stop background jobs, such as those that were started by using the Start-Job cmdlet or the AsJob parameter of any cmdlet. When you stop a background
job, Windows PowerShell completes all tasks that are pending in that job queue and then ends the job. No new tasks are added to the queue after this command is submitted.


This cmdlet does not delete background jobs. To delete a job, use the Remove-Job cmdlet.


Beginning in Windows PowerShell 3.0, Stop-Job also stops custom job types, such as workflow jobs and instances of scheduled jobs. To enable Stop-Job to stop a job with
custom job type, import the module that supports the custom job type into the session before running a Stop-Job command, either by using the Import-Module cmdlet or by using
or getting a cmdlet in the module. For information about a particular custom job type, see the documentation of the custom job type feature.



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=289616
Get-Job
Invoke-Command
Receive-Job
Remove-Job
Resume-Job
Start-Job
Stop-Job
Suspend-Job
Wait-Job
about_Job_Details
about_Remote_Jobs
about_Remote_Variables
about_Jobs
about_Scopes

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>$s = New-PSSession -ComputerName Server01 -Credential Domain01\Admin02
PS C:\>$j = Invoke-Command -Session $s -ScriptBlock {Start-Job -ScriptBlock {Get-EventLog System}}
PS C:\>Invoke-Command -Session $s -ScriptBlock { Stop-job -Job $Using:j }



This example shows how to use the Stop-Job cmdlet to stop a job that is running on a remote computer.

Because the job was started by using the Invoke-Command cmdlet to run a Start-Job command remotely, the job object is stored on the remote computer, and you must use another
Invoke-Command command to run a Stop-Job command remotely. For more information about remote background jobs, see about_Remote_Jobs.

The first command creates a Windows PowerShell session (PSSession) on the Server01 computer and saves the session object in the $s variable. The command uses the credentials
of a domain administrator.

The second command uses the Invoke-Command cmdlet to run a Start-Job command in the session. The command in the job gets all of the events in the System event log. The
resulting job object is stored in the $j variable.

The third command stops the job. It uses the Invoke-Command cmdlet to run a Stop-Job command in the PSSession on Server01. Because the job objects are stored in $j, which is
a variable on the local computer, the command uses the Using scope modifier to identify $j as a local variable. For more information about the Using scope modifier, see
about_Remote_Variables (http://go.microsoft.com/fwlink/?LinkID=252653).

When the command completes, the job is stopped and the PSSession in $s is available for use.






-------------------------- EXAMPLE 2 --------------------------

PS C:\>Stop-Job -Name Job1



This command stops the Job1 background job.






-------------------------- EXAMPLE 3 --------------------------

PS C:\>Stop-Job -ID 1, 3, 4



This command stops three jobs. It identifies them by their IDs.






-------------------------- EXAMPLE 4 --------------------------

PS C:\>Get-Job | Stop-Job



This command stops all of the background jobs in the current session.






-------------------------- EXAMPLE 5 --------------------------

PS C:\>Stop-Job -State Blocked



This command stops all the jobs that are blocked.






-------------------------- EXAMPLE 6 --------------------------

PS C:\>Get-Job | Format-Table ID, Name, Command, @{Label="State";Expression={$_.JobStateInfo.State}},
InstanceID -Auto

Id Name Command State InstanceId
-- ---- ------- ----- ----------
1 Job1 start-service schedule Running 05abb67a-2932-4bd5-b331-c0254b8d9146
3 Job3 start-service schedule Running c03cbd45-19f3-4558-ba94-ebe41b68ad03
5 Job5 get-service s* Blocked e3bbfed1-9c53-401a-a2c3-a8db34336adf

PS C:\>Stop-Job -InstanceId e3bbfed1-9c53-401a-a2c3-a8db34336adf



These commands show how to stop a job based on its instance ID.

The first command uses the Get-Job cmdlet to get the jobs in the current session. The command uses a pipeline operator (|) to send the jobs to a Format-Table command, which
displays a table of the specified properties of each job. The table includes the Instance ID of each job. It uses a calculated property to display the job state.

The second command uses a Stop-Job command with the InstanceID parameter to stop a selected job.






-------------------------- EXAMPLE 7 --------------------------

PS C:\>$j = Invoke-Command -ComputerName Server01 -ScriptBlock {Get-EventLog System} -AsJob
PS C:\>$j | Stop-Job -PassThru

Id Name State HasMoreData Location Command
-- ---- ---- ----------- -------- -------
5 Job5 Stopped True user01-tablet get-eventlog system



This example shows how to use the Stop-Job cmdlet to stop a job that is running on a remote computer.

Because the job was started by using the AsJob parameter of the Invoke-Command cmdlet, the job object is located on the local computer, even though the job runs on the
remote computer. As such, you can use a local Stop-Job command to stop the job.

The first command uses the Invoke-Command cmdlet to start a background job on the Server01 computer. The command uses the AsJob parameter to run the remote command as a
background job.

This command returns a job object, which is the same job object that the Start-Job cmdlet returns. The command saves the job object in the $j variable.

The second command uses a pipeline operator to send the job in the $j variable to Stop-Job. The command uses the PassThru parameter to direct Stop-Job to return a job
object. The job object display confirms that the State of the job is "Stopped".

For more information about remote background jobs, see about_Remote_Jobs.