如何使用 PowerShell 解除安裝 MSI 包?


要使用 PowerShell 解除安裝 MSI 包,我們需要產品程式碼,然後可以使用 msiexec 檔案將產品程式碼與特定應用程式一起用於解除安裝。

可以使用 Get-PackageGet-WmiClass 方法檢索產品程式碼。在此示例中,我們將解除安裝 7-zip 包。

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

上述命令將檢索產品程式碼。要使用 msiexec 解除安裝產品,請在產品 ID 中使用 /x 開關。以下命令將使用上述檢索到的程式碼解除安裝 7-zip。

msiexec /x $product.IdentifyingNumber /quiet /noreboot

這是 cmd 命令,但在 PowerShell 中可以執行,但是我們無法控制此命令的執行。例如,在這之後執行的下一個命令將立即執行。要在解除安裝完成之前等待,可以在 PowerShell 中使用 -Wait 引數使用 Start-Process。

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

要在遠端計算機上執行相同命令,請使用 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
   }

更新於:31-8-2021

24K+ 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.