How to uninstall the MSI package using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

To uninstall the MSI package using PowerShell, we need the product code and then the product code can be used with msiexec file to uninstall the particular application.

Product code can be retrieved using the Get-Package or Get-WmiClass method. In this example, we will uninstall the 7-zip package.

$product = Get-WmiObject win32_product | `
where{$_.name -eq "7-Zip 19.00 (x64 edition)"}
$product.IdentifyingNumber

The above command will retrieve the product code. To uninstall the product using msiexec, use /x switch with the product id. The below command will uninstall the 7-zip using the above-retrieved code.

msiexec /x $product.IdentifyingNumber /quiet /noreboot

This is the cmd command but we can run in PowerShell, however we can’t control the execution of this command. For example, the next command after this would be executed immediately. To wait until the uninstall completes, we can use the Start-Process in PowerShell which uses the -Wait argument.

Start-Process "C:\Windows\System32\msiexec.exe" `
-ArgumentList "/x $($product.IdentifyingNumber) /quiet /noreboot" -Wait

To run the same commands on the remote computers, use Invoke-Command.

Invoke-Command -ComputerName TestComp1, TestComp2 `
   -ScriptBlock {
      $product = Get-WmiObject win32_product | where{$_.name -eq "7-Zip 19.00 (x64 edition)"}
      $product.IdentifyingNumber
      Start-Process "C:\Windows\System32\msiexec.exe" `
      -ArgumentList "/x $($product.IdentifyingNumber) /quiet /noreboot" -Wait
   }
raja
Published on 31-Aug-2021 10:00:51
Advertisements