You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PS Core中Get-Package无法获取已安装软件,求原生Cmdlet替代方案

在PowerShell 7+中获取并卸载已安装软件的原生方案

方案1:直接读取注册表(最可靠,无额外依赖)

Windows系统中已安装软件的核心信息存储在两个注册表路径,用PowerShell原生的Get-ItemProperty即可直接读取,完全避开废弃的WMI/CMI组件:

# 拉取所有带卸载信息的已安装软件
$installedApps = @(
    Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
    Get-ItemProperty -Path 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
) | Where-Object { $_.DisplayName -and $_.UninstallString } | Select-Object DisplayName, DisplayVersion, Publisher, UninstallString, InstallDate

# 按需卸载示例:匹配目标软件并执行卸载
$targetApp = $installedApps | Where-Object { $_.DisplayName -like "*你的软件名称*" }
if ($targetApp) {
    foreach ($app in $targetApp) {
        # 清理卸载字符串里的多余引号
        $cleanUninstallCmd = $app.UninstallString -replace '^"(.+)"$', '$1'
        # 静默卸载(参数根据软件调整,比如/s /qn等)
        Start-Process cmd.exe -ArgumentList "/c $cleanUninstallCmd /quiet /norestart" -Wait -NoNewWindow
    }
}

方案2:用Get-CimInstance读取非废弃类

如果更习惯CIM方式,Win32_AddRemovePrograms类未被废弃,且不会像Win32_Product那样触发MSI自动修复,PowerShell 7+完美支持:

$installedApps = Get-CimInstance -ClassName Win32_AddRemovePrograms | Select-Object DisplayName, Version, Publisher, UninstallString

# 卸载逻辑和上述一致
$targetApp = $installedApps | Where-Object { $_.DisplayName -like "*你的软件名称*" }
if ($targetApp) {
    foreach ($app in $targetApp) {
        $cleanUninstallCmd = $app.UninstallString -replace '^"(.+)"$', '$1'
        Start-Process cmd.exe -ArgumentList "/c $cleanUninstallCmd /quiet /norestart" -Wait -NoNewWindow
    }
}

额外说明

  • 静默卸载参数(/quiet、/norestart)并非通用,不同软件可能使用/s、/qn等参数,需对应调整。
  • 读取注册表需要管理员权限,流水线运行时需确保执行账号具备足够权限。
  • 若需处理UWP应用,使用Get-AppxPackage和Remove-AppxPackage即可,PowerShell 7+完全支持:
# 获取UWP应用列表
$uwpApps = Get-AppxPackage | Select-Object Name, PackageFullName, Publisher

# 卸载指定UWP应用
Get-AppxPackage -Name "目标UWP应用名称" | Remove-AppxPackage

内容的提问来源于stack exchange,提问作者Sebastian Wiszowaty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:45:49