Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


August 22, 2008

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?


RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
The website is down because someone removed the X-Box

What happens when a manager mistakes a server for a games console. ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Microsoft Kills OneCare, Will Launch Free Security Solution

Microsoft on Tuesday announced that it would retire its $50-a-year security subscription product, Windows Live OneCare, and replace it with a free solution codenamed "Morro." Unlike OneCare, however, Morro will focus only on core anti-malware features and ...


Related Events SQL Server 2008 – Can You Wait? | Philadelphia

PowerShell 101 - eLearning Series

SQL Server 2008 – Can You Wait? | Atlanta

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing