Prof. Powershell

Hurry Up and Wait

Some cool cmdlets that can help you with migrating or provisioning computers.

I'm sure you've heard the expression, "Hurry up and wait." You rush, rush, rush and then you sit around waiting. Well sometimes that's a good thing and exactly what you need, especially in a PowerShell script. You might have a series of steps but need to wait on some of them until other processes finish. In the past you probably used some sort of loop and sleep combination. In PowerShell 2.0 we can use Wait-Process:

PS C:\> calc
PS C:\> wait-process calc

I've started Calculator and then called Wait-Process. I've told it to wait for the calc process to end. If you try this your PowerShell prompt won't return to you until Calculator is closed. The process name must be as it shows as the ProcessName in Get-Process. Thus it is ‘calc' not ‘calc.exe' The cmdlet can also wait for multiple processes:

PS C:\> wait-process notepad,calc

PowerShell won't return the prompt until both Notepad and Calculator are closed.
What if you have multiple instances of the same app but want to wait for a specific one to end? Then you need to know the process id:

PS C:\> ps notepad

Handles  NPM(K)  PM(K)  WS(K)  VM(M)  CPU(s)   Id  ProcessName
-------  ------  -----  -----  -----  ------   --  -----------
     70       4    960   5308     60    0.20 4300  notepad
     74       4    960   5704     60    0.26 4880  notepad

PS C:\> Wait-Process -Id 4300

PowerShell will now wait until the first instance of Notepad terminates. The best way to handle this is to start the process or application and pipe it to Wait-Process; then there's no confusion about what process you are waiting for:

PS C:\> start-process calc -passthru | wait-process

Lastly, there may be times that you want to wait, but only for a certain amount of time. If the process is still running then you want PowerShell to move on to the next command:

PS C:\> start-process calc -passthru | wait-process –TimeOut 10

Now we give Calculator 10 seconds to close, otherwise Wait-Process will throw an exception and continue. This is helpful because you can trap the exception and take other action. If you prefer to simply keep going and ignore the exception, then turn off the error pipeline for this expression:

PS C:\> start-process calc -passthru | wait-process –TimeOut 10 –ea SilentlyContinue

As always I encourage you to look at full help and examples for any new cmdlet.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.