PowerShell脚本无法识别已安装的Google Chrome,求解决方法
排查并修复PowerShell脚本无法识别已安装Google Chrome的问题
问题根源
你的脚本依赖Get-WmiObject Win32_Product查询已安装程序,但这个方法有致命局限性:
- 仅能识别通过Windows Installer(MSI)包安装的软件,而现代Google Chrome默认采用非MSI的独立安装程序或Chrome自动更新框架部署,不会出现在Win32_Product的结果集中。
- Win32_Product本身性能差,甚至可能触发不必要的MSI修复操作,不推荐用于软件查询。
排查步骤
确认Chrome安装类型
打开「控制面板→程序和功能」,查看Google Chrome的操作按钮:如果显示「更改」而非「卸载/更改」,说明是典型的非MSI安装;也可检查默认安装路径:C:\Program Files\Google\Chrome\Application(64位)或C:\Program Files (x86)\Google\Chrome\Application(32位)。手动验证注册表信息
打开注册表编辑器(regedit),导航到以下路径,查找包含Google Chrome的子项,确认是否存在UninstallString值:- 64位系统:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall - 32位系统/32位Chrome:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
- 64位系统:
修正后的脚本
方案1:通过注册表获取卸载命令(通用型)
$appName = "Google Chrome" # 定义需要检查的注册表路径 $regPaths = @( "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" ) $uninstallKey = $null foreach ($path in $regPaths) { if (Test-Path $path) { $uninstallKey = Get-ChildItem $path | Where-Object { $_.GetValue("DisplayName") -eq $appName } | Select-Object -First 1 if ($uninstallKey) { break } } } if ($uninstallKey) { $uninstallString = $uninstallKey.GetValue("UninstallString") Write-Output "正在卸载 $appName..." # 调用卸载命令,通过cmd执行 $process = Start-Process -FilePath "cmd.exe" -ArgumentList "/c $uninstallString" -Wait -PassThru -NoNewWindow if ($process.ExitCode -eq 0) { Write-Output "$appName 已成功卸载。" } else { Write-Output "卸载 $appName 失败,退出代码:$($process.ExitCode)" } } else { Write-Output "$appName 未安装在本机。" }
方案2:直接调用Chrome自带卸载参数(更高效)
$appName = "Google Chrome" # 检查Chrome的可能安装路径 $chromePaths = @( "${env:ProgramFiles}\Google\Chrome\Application\chrome.exe", "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" ) $chromeExe = $chromePaths | Where-Object { Test-Path $_ } | Select-Object -First 1 if ($chromeExe) { Write-Output "正在卸载 $appName..." # 调用Chrome的强制卸载参数 $process = Start-Process -FilePath $chromeExe -ArgumentList "--uninstall --force-uninstall" -Wait -PassThru -NoNewWindow if ($process.ExitCode -eq 0) { Write-Output "$appName 已成功卸载。" } else { Write-Output "卸载 $appName 失败,退出代码:$($process.ExitCode)" } } else { Write-Output "$appName 未安装在本机。" }
内容的提问来源于stack exchange,提问作者John Z
相关产品推荐
相关产品推荐

