Suppress the command prompt until one or all of the PowerShell background jobs running in the session are complete.

      Wait-Job [-Id] Int32[] [-Any] [-Force] [-Timeout int] [CommonParameters]

      Wait-Job [-Filter] Hashtable [-Any] [-Force] [-Timeout Int32] [CommonParameters]

      Wait-Job [[-InstanceId] Guid[]] [-Any] [-Force] [-Timeout int] [CommonParameters]

      Wait-Job [-Job] Job[] [-Any] [-Force] [-Timeout int] [CommonParameters]

      Wait-Job [[-Name] string[]] [-Any] [-Force] [-Timeout int] [CommonParameters]

      Wait-Job [-State {NotStarted | Running | Completed | Failed | Stopped | Blocked}]
         [-Any] [-Force] [-Timeout int] [CommonParameters]

       Display the command prompt (and return the job object) when any job completes.
       By default, Wait-Job waits until all of the specified jobs are complete.

   -Filter Hashtable
       Wait for jobs that satisfy all of the conditions established in the associated hash table.
       Enter a hash table where the keys are job properties and the values are job property values.
       This parameter works only on custom job types, such as workflow jobs and scheduled jobs.
       It does not work on standard background jobs, such as those created by using Start-Job.
       (PowerShell 3.0+)

   -Id Int32[]
       Wait for jobs with the specified IDs. 

       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[]
       Wait for jobs with the specified instance IDs.

       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.

   -Job Job[]
       The the jobs to be deleted.
       Enter a variable that contains the job or a command that gets the job.
       You can also pipe a job object to Receive-Job.

   -Name string[]
       Wait for jobs with the specified friendly name(s).
   -State JobState
       Wait for jobs with the specified status.
       Valid values are NotStarted, Running, Completed, Stopped, Failed, and Blocked.

       Continue waiting if jobs are in the Suspended or Disconnected state.
       By default, Wait-Job returns (terminates the wait) when jobs are in one of the
       following states: Completed, Failed, Stopped, Suspended, or Disconnected. 
       (PowerShell 3.0+)        

   -Timeout int
       The maximum wait time for each background job, in seconds.
       The default, -1, waits until the job completes, no matter how long it runs.
       The timing starts when you submit the Wait-Job command.

       If this time is exceeded, the wait will end and the command prompt returns, 
       even if the job is still running. No error message is displayed.

Standard Aliases for Wait-Job: wjb

Wait-Job waits for PowerShell background jobs to complete before it displays the command prompt.
The wait can be for all background jobs or for specific job(s), a maximum wait time can also be set.

When the commands in the job are complete, Wait-Job will display the command prompt and return a job object that can be piped to another command.


Wait for all background jobs running in the session to complete:

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

Use Wait-Job to determine whether a command running as a background job on three different computers is complete:

PS C:\> $sess = new-pssession server01, server02, server03
PS C:\> invoke-command -session $sess -scriptblock {start-job -name Date1 -scriptblock {get-date}}

PS C:\> $done = invoke-command -session $sess -command {wait-job -name Date1}
PS C:\> $done.count

Use Wait-Job to determine when the first of many background jobs running in the current session are complete (local jobs running on remote machines):

PS C:\> $sess = new-pssession (get-content machines.txt)
PS C:\> $commands = 'get-eventlog -log system | where {$_.EntryType -eq "error" -and $_.Source -eq "LSASRV"} | out-file errors.txt'

PS C:\> invoke-command -session $sess -scriptblock {param($commands)start-job -scriptblock {$commands}} -ArgumentList $commands
PS C:\> invoke-command -session $sess -scriptblock {wait-job -any}

Identify three jobs by their IDs and wait until any of them are complete:

PS C:\> wait-job -id 1,2,7 -any

Wait 120 seconds (two minutes) for the ss64 job to complete:

PS C:\> wait-job -name ss64 -timeout 120

Run a Get-Process command in each of three PSSessions against remote machines, using -AsJob, the job is created on the local computer and the results are automatically returned to the local computer, the Wait will return when all 3 jobs are complete:

PS C:\> $sess = new-pssession Server64, Server65, Server66
PS C:\> $myjob = invoke-command -session $sess -scriptblock {get-process} -asjob
PS C:\> $myjob | wait-job

“A girl can wait for the right man to come along but in the meantime that still doesn’t mean she can’t have a wonderful time with all the wrong ones” ~ Cher

Related PowerShell Cmdlets

Get-Job - Get PowerShell background jobs that are running.
Invoke-Command - Run command.
Stop-Job - Stop a PowerShell background job.
Wait-Event - Wait until a particular event is raised.
Wait-Process - Wait for a process to stop.

Copyright © 1999-2024 SS64.com
Some rights reserved