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

如何用PowerShell从注册表获取指定关键词的卸载字符串?

问题分析与解决方法

原代码无法正常工作的核心问题有两个:

  1. 错误使用-contains运算符:-contains用于判断集合是否包含某个元素,而非检查字符串是否包含子串。要匹配包含指定关键词的字符串,应该用-like(通配符匹配)或-match(正则匹配)。
  2. 未覆盖32位程序的注册表路径:64位Windows系统中,32位程序的卸载信息通常存放在HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\路径下,只检查默认路径会漏掉这类条目。

修正后的PowerShell代码

# 定义需要检查的注册表路径(同时覆盖64位和32位程序)
$uninstallPaths = @(
    "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
    "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
)

# 遍历路径,过滤符合条件的条目并提取指定属性
Get-ItemProperty $uninstallPaths | 
    Where-Object {
        # 先排除DisplayName为空的项,避免后续匹配出错
        $null -ne $_.DisplayName -and
        # 匹配同时包含"jump client"和"cloud"的DisplayName(不区分大小写)
        $_.DisplayName -like "*jump client*" -and 
        $_.DisplayName -like "*cloud*"
    } | 
    Select-Object DisplayName, UninstallString

额外说明

  • 如果需要区分大小写的匹配,可以改用-clike(对应-like)或-cmatch(对应-match)。
  • 若要直接执行卸载命令,可以在提取UninstallString后,通过Invoke-Expression或直接调用命令(注意处理命令中的引号,比如拆分带引号的路径)。例如:
    # 提取后执行卸载(示例,需根据实际情况调整)
    $uninstallItems = Get-ItemProperty $uninstallPaths | Where-Object { $null -ne $_.DisplayName -and $_.DisplayName -like "*jump client*" -and $_.DisplayName -like "*cloud*" }
    foreach ($item in $uninstallItems) {
        # 处理带引号的卸载命令,避免解析错误
        $uninstallCmd = $item.UninstallString -replace '^"([^"]+)"(.*)', '$1$2'
        Start-Process -FilePath $uninstallCmd -Wait
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 05:55:14