Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 22, 2008 12:00 AM

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?

Windows IT Pro
InstantDoc ID #100102
Rating: (2)

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:

[diagnostics.process]::start("notepad.exe").WaitForExit()

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

[diagnostics.process]::start("notepad.exe","C:\temp\john.txt").WaitForExit()

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.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.