Q. When I run a Win32 process from Windows PowerShell, how can I pause PowerShell so that the process can exit before I run other PowerShell commands?

By default, if you launch a Win32 process from PowerShell, control returns immediately to the PowerShell prompt and doesn’t wait for the process to terminate. You can see this if you launch Notepad: Notepad starts and runs, but you get control back immediately in the PowerShell window.

If you run a console application, PowerShell will wait for the console application to finish before continuing. Thus, to make PowerShell wait for a Win32 process to finish, you can pipe the output of the process to null, like this:

notepad | out-null

When you run this command, control doesn’t return to PowerShell until you close the Notepad instance that was starting.

Another technique is to use the Process.WaitForExit method to tell PowerShell to wait while the process finishes running, which means that you need to get a link to the process ID of the Win32 process you want to wait for. Although you can try to obtain the process ID by searching for the process name using criteria such as window titles (e.g. $tpid = get-process | where \{$_.mainwindowtitle -match "notepad"\}), that method is error prone if multiple processes are running with the same name. A better method for obtaining the process ID is to create the process by using the Microsoft .NET Framework System.Diagnostics.Process class's start method, which returns the process ID as it creates the process, as in this example:

PS C:\Users\Administrator> $tpid = \[diagnostics.process\]::start("notepad.exe")
PS C:\Users\Administrator> $tpid.WaitForExit()

Now control won’t return to PowerShell until the Notepad instance is closed. An alternative to the previous techniques is to combine the commands into one, like this:


Using Process.WaitForExit, we can also enter a maximum number of milliseconds to wait. For example running $tpid.WaitForExit(30000) waits for the process to close or 30 seconds, whichever happens first. If the process closes within the specified time, Process.WaitForExit returns a TRUE status; otherwise it returns FALSE.

Note that if you need to pass parameters to the Win32 process, simply add them as a second argument to the start command. For example, to start Notepad editing file c:\temp\john.txt, I’d use the command


Note that you need to pass the full path to any parameters. If you want to use only the current working folder, specify just $pwd.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.