EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Wait
 

PowerShell Wait

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated March 4, 2023

PowerShell Wait

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

PowerShell Wait 1

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"

powershell 2

You can also provide the timeout parameter in seconds if the process doesn’t terminate in the specified time, it will throw an error.

powershell 3

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"

write output

Start-Sleep -Seconds 5

Start sleep

Write-Output “This command will be executed after 5 seconds”

You can also provide the -milliseconds parameter.

Start-Sleep -Milliseconds 5000

powershell 4

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"

write output 1

Output:

terminal

Users can interrupt in the timeout specified if you want uninterrupted Timeout then use /NoBreak switch.

terminal 1

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"

start job

Output:

Wait-Job

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

wait event 1

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

Wait-Event;
New-Event -SourceIdentifier Timer -Sender windows.timer -MessageData "Test" | Out-Null

Output:

write output 2

If we have any particular source for that event, we can specify that SourceIdentifier Name.

Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 200

wait event 2

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

Restart-Computer -ComputerName test1-win2k12 -Wait -Force
Write-Output "Get Process command will be executed after server restart"
Get-Process -ComputerName test1-win2k12

Output:

Wait Parameter

There are other commands which by default include the Wait Parameter like Start-AzVM and Stop-AzVM command.

start AvZM

Start-AzVM -Name TestVm -ResourceGroupName TestRG -NoWait
Write-Output "This command will be executed immediately"

To Stop VM,

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 –

  1. PowerShell Execution Policy
  2. PowerShell Sleep
  3. PowerShell SubString
  4. PowerShell scriptblock

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW