How to pause PowerShell until the external process finished?

By default, if you launch a Win32 process from PowerShell, control returns immediately to the PowerShell and doesn’t wait for the process to terminate.

We can use [diagnostics.process] to start a Window’s batch job (or application) and wait until the job finished/terminated:
$batfile = [diagnostics.process]::Start("D:\Demo\My_Script.bat")
$batfile.WaitForExit()

Then the Powershell script resumes with the next line.

One thought on “How to pause PowerShell until the external process finished?

  • Thursday September 19th, 2013 at 09:29
    Permalink

    How can I have powershell run the silent un-install first wait till it is finished then run the install? After researching I did the below which works, but the blank Notepad window pops up and I had to close it. I don’t want the user to get any notepad window when I deploy this to them, simply uninstall and then install thank you.

    Get-WmiObject -Class Win32_Product | Where-Object {$.Name -eq “On-Screen Takeoff”} | foreach- object -process {$.Uninstall()}

    Notepad.exe | Out-Null

    $arguments=”/quiet” Start-Process “\davisconstruction.com\ROOT\Installs\OnCenter\OST\Testverion3906\ost3906.msi” $arguments

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *