11

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?

J W
  • 165
  • 1
  • 2
  • 7

3 Answers3

31

Start-Process has a wait parameter:

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

The solution to restart after an misexec.exe uninstallation is to add the /forcerestart parameter to the msiexec call instead of trying to restart in powershell (Credits to Matt):

Start-Process C:\Windows\System32\msiexec.exe -ArgumentList @("/uninstall {90120000-0030-0000-0000-0000000FF1CE}", "/forcerestart")
Paul
  • 5,524
  • 1
  • 22
  • 39
  • In case you need to use a filename instead of a GUID and that filename has spaces in it: `Start-Process -FilePath "C:\Windows\System32\msiexec.exe" -ArgumentList '/i "C:\Temp\SQLSysClrTypes 15.0.2000.5.msi" /qr' -NoNewWindow -Wait` – Slogmeister Extraordinaire Feb 03 '20 at 19:56
  • The "-wait" is not always functional; Sometime sthe job ignore the wait, like in invoke-command -scriptblock. - NoT sure why, yet... – Patrick Burwell Sep 22 '22 at 18:21
12

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    
Eric Cote
  • 851
  • 1
  • 7
  • 7
  • 1
    This should be the answer. However. I would pipe to `Write-Verbose` so that you can inspect the output if so required. – Jani Hyytiäinen Sep 19 '21 at 08:54
  • +1 for this answer althought the current accepted answer (Start-Process) is more explicit in intention. I like this solution though so I still used it. – Mike Smith Nov 28 '22 at 16:49
0

My suggestion for this is to actually get the Office Removal Tool from Microsoft and extract the VBS script from it. Run that in a Start-Process with the -wait argument, and reboot afterwards. It will not only attempt to gracefully remove Office with msiexec as you are doing, it will also go back and clean up any straggling files or registry entries in case the application is corrupt and will not uninstall nicely.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56