views:

130

answers:

2

Hi,

Is there a way of suspending a PowerShell script until some action has been completed. For example if I was using a Linux shell and I typed gedit then gedit would load but the shell would be suspended until I closed gedit (unless '&' was added to the end of the command).

Is there anyway to mimick this behaviour with PowerShell? So could I get a script to open Notepad but suspend itself until Notepad was closed again.

Thanks...

+1  A: 

You need to use the System.Diagnostics.Process class and call the .WaitForExit method after setting the appropriate ProcessInfo properties to launch Notepad.

guillermooo
This is the only way we could figure it out as well, oddly though it does not work for msi's. I think because of the msi service, and multiple msi processes involved it just returns no matter what. Be interesting to see if someone else has a different way.
Alex
Thanks guillermooo combined with the second part of Keiths response that works as well, just a little more long winded.
Jonny
+4  A: 

Just pipe to Out-Null and PowerShell will wait until the windows app closes:

PS> notepad | out-null

Or as the previous poster points out:

PS> (Start-Process notepad -PassThru).WaitForExit()
Keith Hill
Thanks Keith, I've found that the out-null works the best. It's just what I was looking for.
Jonny