12
votes

I am new to Powershell and don't have much of a programming background, just trying to use it for software packaging. Anyway, I found out about the start-process command with the -Wait parameter, and it works great for most things. What I noticed though is that not only does it wait for the process you specify, it waits for any sub processes, even after the main process is no longer running. Usually this is good, but I have a weird situation. I am running a complex Oracle batch file with start-process. The batch file eventually runs setup.exe which is what I really want to wait for, but other processes spawn too that never stop. So if I use the -wait parameter, the script never stops even after setup.exe is no longer running. The closest I've found is using this:

saps -FilePath "Path\config.bat" -ArgumentList "arguments"
Start-Sleep -s 10
Wait-Process -Name "setup"

This works, but I would think there'd be a better way without having to use a timeout command. Any ideas?

2
$process = Start-Process $path "arguments" -PassThru; $process.WaitForExit($timeoutMS) - Mathias R. Jessen
Although nearly identical to the accepted answer, the comment by @MathiasR.Jessen above worked for me properly. Putting the Start-Process assignment on a separately line ran it and ignored the WaitForExit() command. - whatisit

2 Answers

22
votes

you can use this command easy :

$myprocss = Start-Process "powershell" -PassThru 
$myprocss.WaitForExit()

this command will continue when process end .

7
votes

Using Start-Process, there's an option for waiting:

Start-Process -Wait -FilePath "do_things.exe" -ArgumentList "argument another_argument"