如何使用 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 中的 Start-Process,它使用-Wait参数。

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
   }

猜你喜欢