Updated March 4, 2023
Definition of PowerShell Wait
PowerShell wait cmdlets are used to hold the execution for some certain period to wait for the command, job, or Process to complete, to notify a user that background job is running and ask to wait for some specific period, to restarting remote systems, and wait until it comes up, starting and stopping VMs, etc, and that depends on the command specified. If the command is for the background Job to process first then it will wait to finish and if the command is to halt the execution for a specific period then the user has to wait until the time specified.
Syntax:
a. Wait-Process
The above command is used to wait for the processes to be stopped before accepting the new input or moving to the next commands.
b.Wait-Job
This command is used to complete the background job and execution won’t move to the next stage until the execution is finished.
c. Start -Sleep
This command halts the script execution for the specified time in seconds or milliseconds and can’t make any command or job to wait in the background but pauses the script execution.
d. Wait-Event
This command suspends the execution of the script or function until the particular event is raised and continues the execution of the script after that.
e. Wait Parameters
Many commands use the -Wait parameter directly in the command to wait for that command to finish instead of using the Wait-Job command running separately.
f. Timeout
Similar to the Start-Sleep command, this command holds the execution of the script for the specified period.
How Wait Method works in PowerShell?
Once we use the Wait commands, it stops the execution until the background job or process completes and resumes the execution from the last point where it had stopped. Some commands like Start-Sleep and timeout, just pause the script but don’t run any job or process in the background.
Examples
Let us discuss examples of PowerShell Wait.
1. Wait-Process Example
When we use the command Wait-Process, it waits for the particular process or application to be stopped. This command accepts the Process ID, Name, and the Timeout in seconds.
For example, we have the SnippingTools application running and we are going to terminate it but we will wait until the process is terminated.
Get-Process SnippingTool
To use the wait command we can either use ProcessID or the Name of the process.
Wait-Process -ID 32328
Write-Output "This command will be executed after process termination"
You can also provide the timeout parameter in seconds if the process doesn’t terminate in the specified time, it will throw an error.
You can also provide the multiple process names for the Wait-Process command.
2. Start-Sleep Example
This command holds the execution for a certain amount of time and the next command waits until then. You can provide time in Seconds or milliseconds.
Write-Output "Writing First Line"
Start-Sleep -Seconds 5
Write-Output “This command will be executed after 5 seconds”
You can also provide the -milliseconds parameter.
Start-Sleep -Milliseconds 5000
c. Timeout Example
This is a cmd command but can be accessible in PowerShell as well. Once you specify the Timeout in seconds, the next commands wait until the timeout.
Write-Output "Writing First Line"
Timeout /T 5
Write-Output "This command will be executed after 5 seconds"
Output:
Users can interrupt in the timeout specified if you want uninterrupted Timeout then use /NoBreak switch.
The only option with the /NoBreak switch is to press Ctrl+C to terminate further execution.
d. Wait-Job Example
When we use the Wait-Job command, it waits for the scriptblock to complete and executes the next line command.
$sb = Start-Job -ScriptBlock{
$i = 0
while($i -lt 10){
$i++
Sleep 3
}
}
Wait-Job $sb.Name
Write-Output "This line will be executed after job completes"
Output:
We can also specify the timeout for the job to complete using the -Timeout parameter in seconds in the Wait-Job cmdlet. If the job doesn’t complete during that period, the script will throw an error.
e. Wait-Event Example
This command waits for the particular event to be raised and PowerShell suspends the execution until then and resumes when the event triggers.
Wait-Event
The above command waits for any next event to trigger and suspends the PowerShell execution for that time.
We will create a new test event here to trigger the Wait-Event command.
Wait-Event;
New-Event -SourceIdentifier Timer -Sender windows.timer -MessageData "Test" | Out-Null
Output:
If we have any particular source for that event, we can specify that SourceIdentifier Name.
Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 200
g. -Wait Parameter in Commands
Few commands like Restart-Computer, Start-AZVM, Stop-AzVM, etc support the -Wait parameter and the next commands execute after executing the -Wait parameter command. For example,
Restart-Computer -ComputerName test1-win2k12 -Wait -Force
Write-Output "Get Process command will be executed after server restart"
Get-Process -ComputerName test1-win2k12
Output:
There are other commands which by default include the Wait Parameter like Start-AzVM and Stop-AzVM command.
Start-AzVM -Name TestVm -ResourceGroupName TestRG -NoWait
Write-Output "This command will be executed immediately"
To Stop VM,
Stop-AzVM -Name TestVm -ResourceGroupName TestRG
Write-Output "This command will be executed after VM Stops"
In the first command Start-AZVM, the -NoWait parameter is specified so that command won’t wait to start the VM but in the second command Stop-AZVM, the default parameter is Wait so that the next command will wait until the VM stops successfully.
Conclusion
There are many Wait cmdlets in PowerShell and supported Wait parameter in PowerShell commands so that the previous commands should complete first because sometimes the next command input depends on the output of the previous command and we need them to be executed first.
Recommended Articles
This is a guide to PowerShell Wait. Here we discuss the Definition, How Wait Method works in PowerShell and examples with code implementation. You may also have a look at the following articles to learn more –