August 22, 2008 02:22 PM

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?

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

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.

ARTICLE TOOLS

Add a Comment

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

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS

eBooks

Playbook for a Virtualized Datacenter | During challenging times, optimizing the IT infrastructure becomes imperative. Many organizations are looking to extend their virtualization efforts to encompass the entire datacenter. Get a step-through of your technology options and more.

White Papers

As the advances in USB devices have made them invaluable to most business users’, they have also exposed organizations to enormous risks. Learn how to easily enforce device/port control and data encryption policies without requiring new infrastructure and additional admin overhead.

WEB SEMINARS

Is Flexible Lockdown Possible? Join Darren Mar-Elia for this free web webinar exploring the various methods you should consider to protect and control your desktops while also understanding the impact on the end user community.
View Seminar On-Demand.

eLearning Series

We bring the experts direct to you to share their real-world perspective, experience, and expertise. During each event, three sessions stream in real time, allowing you to learn, to ask questions, and to get solutions.
Upcoming event: Windows 7 Deployment

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.