41

I need to call an external application (i.e. & 'Notepad' ) and then get the process ID of the called application.

Get-Process Notepad = will return all Notepad processes

I want to do something like:

$objApp = & 'c:\Notepad.exe'

WHILE (get-process -ID $objApp.id | select -property Responding) {
  Start-Sleep -s 10
  Echo "STILL WAITING"
}
Echo "Done!!"
Ian
  • 55
  • 4
Schlauge
  • 483
  • 1
  • 4
  • 7

2 Answers2

72

Use Start-Process with the -PassThru argument like this:

$app = Start-Process notepad -passthru
Wait-Process $app.Id
zdan
  • 28,667
  • 7
  • 60
  • 71
  • 3
    @JohnDemetriou It tells Start-Process to output the process object onto the pipeline (i.e. "to pass it thru the pipeline") – zdan Apr 08 '16 at 17:52
13

More succinct:

# Starts Notepad and returns the ID
(Start-Process Notepad -passthru).ID
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82