19

I want to execute multiple external scripts in PowerShell simultaneously and then wait for all of them to finish before proceeding. Currently I am using 'start-process -NoNewWindow ...' commandlet that loops through all child processes but then terminates.

I have found many ways to wait for 1 process to finish (this is obviously trivial) but none of them seem to work as a solution for my problem.

Having an equivalent for UNIX version in PowerShell would definitely be what I am looking for.

Community
  • 1
  • 1
Jacek M
  • 2,349
  • 5
  • 22
  • 47

2 Answers2

35

And don't forget about probably the most straight forward way to do this for mulitple processes - Wait-Process e.g.:

$procs = $(Start-Process Notepad.exe -PassThru; Start-Process Calc.exe -PassThru)
$procs | Wait-Process
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 2
    +1 Keith. I completely missed wait-process. May be because I never used it :) – ravikanth Feb 14 '11 at 17:14
  • Yeah, it is a handy cmdlet from time to time. :-) – Keith Hill Feb 14 '11 at 17:26
  • `Wait-Process` works flawlessly for multiple processes as well, however, I guess you cannot report real-time status information like number of currently running child processes and the only control you get is `Timeout` parameter. – Jacek M Feb 15 '11 at 08:21
  • 4
    +1 Remember the `-PassThru` parameter, otherwise `$procs` will be null! – Sune Rievers Jun 02 '14 at 07:27
9

Did you check the background jobs feature in PowerShell v2?

$job = Start-Job -Name "Proc1" -ScriptBlock { Sleep 10000 }
Wait-Job -Job $job

Also, Start-Process has a parameter called -wait. When specified, Start-Process will wait for the child to exit.

Start-Process -FilePath calc.exe -Wait
ravikanth
  • 24,922
  • 4
  • 60
  • 60
  • -Wait switch is not really helpful when dealing with multiple processes. I managed to solve my problem using jobs, they're a great tool. Thank you! – Jacek M Feb 14 '11 at 12:24