Monthly Archives: October 2009

How To Start A Process And Wait For It To Complete? #PowerShell

One of the new Cmdlets introduced with PowerShell v2 is called Start-Process. It starts a process on the local computer and optionally waits for it to complete if you use the Cmdlet’s Wait switch parameter. Start-Process is implemented by using the Start method of the System.Diagnostics.Process class of the NET Framework.

The first release of Windows PowerShell didn’t came with a Cmdlet like Start-Process. The function below, it is called Start-Process as well, waits for process completion by default. Additionally, the function will wait for all child processes of the process to complete if you use the WaitForChildProcesses switch parameter. Furthermore, the function will not wait infinitely since it supports a Timeout parameter. By default, it will wait 600 seconds for a process (and child processes) to complete. Like Posh v2’s Start-Process the function uses the System.Diagnostics.Process NET Framework class.

So, why wait for child processes? If it is important to wait for process completion, maybe waiting for its child processes to complete matters as well. For example, the Oracle installer invokes Java and terminates while the Java Runtime is still busy installing the software.

Furthermore, I consider a Timeout as mandatory to avoid an infinite waiting due to a hanging process (or child process).

BTW, of course you can use the function in PowerShell v2 too. Remember that the function has two advantages: wait for child processes, and timeout!