可以使用 Win32_Product 类远程或本地安装 Windows Installer 程序包。

注意:若要安装应用程序,必须使用“以管理员身份运行”选项启动 PowerShell。

在远程安装时,请使用通用命名约定 (UNC) 网络路径指定 .msi 包的路径,因为 WMI 子系统并不了解 PowerShell 路径。 例如,若要在远程计算机 PC01 上安装位于网络共享 \AppServ\dsp 中的 NewPackage.msi 包,请在 PowerShell 提示符下键入以下命令:

Invoke-CimMethod -ClassName Win32_Product -MethodName Install -Arguments @{PackageLocation='\AppSrv\dsp\NewPackage.msi'}

不使用 Windows Installer 技术的应用程序可能具有用于自动部署的特定于应用程序的方法。 查看应用程序的文档或咨询应用程序供应商的支持系统。

删除应用程序

通过使用 PowerShell 删除 Windows Installer 程序包与安装程序包的方式大致相同。 下面是一个根据其名称选择要卸载的程序包的示例;在某些情况下,使用 IdentifyingNumber 进行筛选可能会更容易:

Get-CimInstance -Class Win32_Product -Filter "Name='ILMerge'" | Invoke-CimMethod -MethodName Uninstall

即使是在本地执行操作,删除其他应用程序也并不那么简单。 我们可以通过提取 UninstallString 属性查找这些应用程序的命令行卸载字符串。 此方法适用于 Windows Installer 应用程序和显示在 Uninstall 项下方的旧程序:

Get-ChildItem -Path Uninstall: | ForEach-Object -Process { $_.GetValue('UninstallString') }

如果愿意,你可以按显示名称筛选输出:

Get-ChildItem -Path Uninstall: |
    Where-Object -FilterScript { $_.GetValue('DisplayName') -like 'Win*'} |
        ForEach-Object -Process { $_.GetValue('UninstallString') }