Wait For a Program to Finish

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

You should use Start-Process and its -Wait parameter if you want to force PowerShell to wait for a program to finish before it continues:

Start-Process notepad -Wait

This will cause PowerShell to suspend until you close Notepad. You should access the program with Get-Process and call its method WaitForExit() if you want to wait for programs that you did not launch yourself, or wait for a program after you launched it. The next line will wait for all running Notepad processes to exit before PowerShell continues:

Get-Process notepad | Foreach-Object { $_.WaitForExit() }

 

 

Twitter This Tip! ReTweet this Tip!


Posted Mar 17 2011, 08:00 AM by ps1

Comments

dwknight wrote re: Wait For a Program to Finish
on 05-12-2011 12:55 PM

The 'Start-Process xyz -Wait' construct will sometimes fail to wait if what is being called is a more complex application with subprocesses rather than a simple process.  I first ran into this when trying to script PSP updates from HP.

The solution is to execute the application and pipe it to out-null, like this:

& myapp.exe | out-null

Copyright 2012 PowerShell.com. All rights reserved.