本帖最后由 小白龙 于 2025-10-2 10:49 编辑
用下面的方法解决了, 但是有更简单的吗?- # 定义浏览器在注册表中显示的名称
- $registryNames = @("Google Chrome", "Mozilla Firefox", "Microsoft Edge", "Internet Explorer")
- # 创建一个空数组,用于存放结果(包含名称和路径)
- $installedBrowsers = @()
- # 遍历两个主要的注册表位置(32位和64位程序)
- $registryPaths = @(
- "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*",
- "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
- )
- # 获取所有已安装程序的信息
- $installedPrograms = Get-ItemProperty $registryPaths -ErrorAction SilentlyContinue
- # 检查每个目标浏览器是否在已安装程序列表中
- foreach ($name in $registryNames) {
- # 找到匹配的程序项
- $program = $installedPrograms | Where-Object { $_.DisplayName -eq $name }
-
- if ($program) {
- # 将注册表名称映射回友好的名称和可执行文件名
- $browserInfo = switch ($name) {
- "Google Chrome" {
- @{ Name = "Google Chrome"; Executable = "chrome.exe"; Path = $program.InstallLocation }
- }
- "Mozilla Firefox" {
- @{ Name = "Mozilla Firefox"; Executable = "firefox.exe"; Path = $program.InstallLocation }
- }
- "Microsoft Edge" {
- @{ Name = "Microsoft Edge"; Executable = "msedge.exe"; Path = $program.InstallLocation }
- }
- "Internet Explorer" {
- # IE的路径比较特殊,通常在系统目录
- @{ Name = "Internet Explorer"; Executable = "iexplore.exe"; Path = "$env:ProgramFiles\Internet Explorer" }
- }
- }
- # 将找到的浏览器信息添加到结果数组中
- $installedBrowsers += $browserInfo
- }
- }
- # 输出结果
- if ($installedBrowsers.Count -gt 0) {
- Write-Host "已检测到以下浏览器及其安装路径:" -ForegroundColor Green
- # 遍历结果数组,格式化输出
- $installedBrowsers | ForEach-Object {
- # 检查路径是否存在,如果不存在则提示
- $pathStatus = if (Test-Path $_.Path) { " (存在)" } else { " (路径未找到)" }
- Write-Host "- $($_.Name): $($_.Path)$pathStatus"
- }
- } else {
- Write-Host "未检测到常见浏览器的安装。" -ForegroundColor Yellow
- }
复制代码 |