Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make PowerShell wait until a command is complete before proceeding?

I'm using the following line to uninstall office 2007 based on its Product ID

Start-Process C:\Windows\System32\msiexec.exe -ArgumentList "/uninstall {90120000-0030-0000-0000-0000000FF1CE}"

I'd like to force a reboot after the uninstall is complete however using -Wait or piping the results to Out-Null don't wait until the uninstall is complete before processing the next line which is a restart. I've also tried using cmd to uninstall but with the same result.

cmd /c "msiexec.exe /uninstall {90120000-0030-0000-0000-0000000FF1CE}"

Is there any way to force powershell to wait until the uninstall is complete before processing the Restart-Computer command? I was thinking possibly writing something that detects when the setup.exe process stops before proceeding to the restart?

like image 855
J W Avatar asked Oct 21 '14 17:10

J W


People also ask

How do I make a wait in PowerShell?

In PowerShell, we can use the Start-Sleep cmdlet to suspend/pause/sleep/wait the activity in a script or session for the specified period of time. You can use it for many tasks, such as waiting for an operation to be completed or pausing before repeating an operation.

How do I set a timer in PowerShell?

You just need to use New-Object to create a stopwatch! It's as easy as 1, 2, $stopWatch = New-Object -TypeName System. Diagnostics. Stopwatch !

How do I pause a PowerShell script for 10 seconds?

Start-Sleep Usage To pause the script for 10 seconds, I'd just use Start-Sleep -Second 10 . If I want to get anal about things, I could also specify the time in milliseconds as Start-Sleep -Milliseconds 10000.

Does PowerShell have a pause?

PowerShell Pause cmdlets are used to halt the script's execution for a certain period, wait for the user inputs to enter and then proceed with the execution, slow down the speed of the execution, or wait for another process or job to complete first and resume execution.


1 Answers

The simplest workaround: pipe the result. This will force a wait until the process is finished.

No need for start-process or cmd.

You could pipe to out-default, out-file, out-null or out-host, depending on how you want to handle the output. (If you don't care about the output, simply use out-null.)

& msiexec.exe /uninstall "{90120000-0030-0000-0000-0000000FF1CE}" | Out-Null    
like image 129
Eric Cote Avatar answered Oct 22 '22 10:10

Eric Cote