Stop-Job

Stop a PowerShell background job.

Syntax
      Stop-Job [-Job job[]] [-PassThru] [-Confirm] [-WhatIf] [CommonParameters]

      Stop-Job [[-InstanceId] Guid[]] [-PassThru] [-Confirm] [-WhatIf] [CommonParameters]

      Stop-Job [[-Name] string[]] [-PassThru] [-Confirm] [-WhatIf] [CommonParameters]

      Stop-Job [[-Id] Int32[]] [-PassThru] [-Confirm] [-WhatIf] [CommonParameters]

      Stop-Job [-State {NotStarted | Running | Completed | Failed | Stopped | Blocked}] [CommonParameters]

Key
   -Job Job[]
       The jobs to be stopped.
       Enter a variable that contains the jobs or a command that gets the jobs.
       Jobs can also be piped to Stop-Job.
       By default, will delete all jobs that were started in the current session.

    -Id Int32[]
       Stop only jobs with the specified IDs. The default is all jobs in the current session.

       The ID is an integer that uniquely identifies the job within the current session.
       It is easier to remember and to type than the instance ID, but it is unique only
       within the current session. You can type one or more IDs (separated by commas).
       To find the ID of a job, type "Get-Job" without parameters.

   -InstanceId Guid[]
       Stop jobs with the specified instance IDs. The default is all jobs.

       An instance ID is a GUID that uniquely identifies the job on the computer.
       To find the instance ID of a job, use Get-Job.

   -Name string[]
       Stop the job(s) with the specified friendly names.
       Enter a job name, or use wildcard characters to enter a job name pattern.
       By default, stops all jobs in the current session.
       The friendly name is not guaranteed to be unique, use the -WhatIf and -Confirm parameters

   -PassThru
Return an object representing the new background job. By default, this cmdlet does not generate any output. -State JobState Stop only jobs in the specified state. Valid values are NotStarted, Running, Completed, Stopped, Failed, and Blocked. -Confirm Prompt for confirmation before executing the command. -WhatIf Describe what would happen without actually executing the command. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.

Standard Aliases for Stop-Job: spjb

Stop-Job stops a PowerShell background job on the local computer. It does not delete background jobs. To delete a job, use Remove-Job.

Stopping a job will complete all tasks that are pending in that job queue and then end the job.

Examples

Stop all jobs with a State value of "Failed":

PS C:\> stop-job -state failed

Stop all the background jobs in the current session:

PS C:\> get-job | stop-job

Get the InstanceIDs of all jobs:

PS C:\> get-job | format-table ID, Name, Command, InstanceID -auto

Get the the (calculated) job state of all jobs:

PS C:\> get-job | format-table ID, Name, Command, @{Label="State";Expression={$_.jobstateinfo.state}} -auto

Stop a background job with a given InstanceID:

PS C:\> stop-job -instanceid e4bbfed1-9c64-401a-a2c3-a7db34336cdf

Start a job remotely using Invoke-Command and then stop the job that is running on a remote computer. Because the job is local, the "param" keyword is required to declare the local variables, and the -ArgumentList parameter can then be used to supply values for the variables:

PS C:\> $s = new-pssession -computername Server64 -credential domain64\admin01
PS C:\> $j = invoke-command -session $s -scriptblock {start-job -scriptblock {get-eventlog system}}

PS C:\> invoke-command -session $s -scriptblock {param($j) stop-job -job $j} -ArgumentList $j

Start a job on the local computer, which runs on Server64 (using -AsJob makes the remote command run as a background job), then stop the job with a local Stop-Job command:

PS C:\> $myjob = invoke-command -computername Server64 -scriptblock {get-eventlog system} -asjob

PS C:\> $myjob | stop-job -passthru

“The beautiful thing about learning is nobody can take it away from you” ~ B. B. King

Related:

Get-Job - Get PowerShell background jobs that are running
Invoke-Command - Run command


© Copyright SS64.com 1999-2014
Some rights reserved