Windows环境下PowerShell设置Chrome为默认浏览器失败求助
解决Windows 10/11中默认浏览器频繁恢复为Edge的PowerShell方案
原脚本失效原因
仅修改HKLM:\SOFTWARE\Classes\ChromeHTML\shell\open\command不足以覆盖Windows的默认应用机制:
- Windows 10/11优先使用用户级(HKCU)的默认关联设置,而非机器级(HKLM)
- Edge内置默认浏览器恢复逻辑,会自动重置关联
- 部分协议/文件类型需要单独设置关联
有效PowerShell脚本
以下脚本需以管理员身份运行,覆盖Chrome作为默认浏览器的核心关联,并禁用Edge的自动恢复:
# 定义Chrome路径(根据实际安装位置调整,64位Chrome路径为"C:\Program Files\Google\Chrome\Application\chrome.exe") $chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" $chromeArgs = "--single-argument %1" # 检查Chrome是否存在 if (-not (Test-Path $chromePath)) { Write-Error "Chrome未找到,请检查路径是否正确" exit 1 } # 检查是否以管理员身份运行 $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Error "请以管理员身份运行此脚本" exit 1 } # 设置Chrome为HTML文件和HTTP/HTTPS协议的默认关联(机器级) $fileTypes = @("HTML", "HTM") $protocols = @("HTTP", "HTTPS") foreach ($type in $fileTypes + $protocols) { $classKey = "HKLM:\SOFTWARE\Classes\$type" $commandKey = "$classKey\shell\open\command" # 创建路径(如果不存在) if (-not (Test-Path $commandKey)) { New-Item -Path $commandKey -Force | Out-Null } # 设置默认值 Set-ItemProperty -Path $classKey -Name "(Default)" -Value "ChromeHTML" Set-ItemProperty -Path $commandKey -Name "(Default)" -Value "`"$chromePath`" $chromeArgs" } # 设置用户级默认关联(覆盖HKLM,确保当前用户生效) $userClassKey = "HKCU:\SOFTWARE\Classes\ChromeHTML\shell\open\command" if (-not (Test-Path $userClassKey)) { New-Item -Path $userClassKey -Force | Out-Null } Set-ItemProperty -Path $userClassKey -Name "(Default)" -Value "`"$chromePath`" $chromeArgs" # 禁用Edge的默认浏览器自动恢复 $edgePolicyKey = "HKLM:\SOFTWARE\Policies\Microsoft\Edge" if (-not (Test-Path $edgePolicyKey)) { New-Item -Path $edgePolicyKey -Force | Out-Null } Set-ItemProperty -Path $edgePolicyKey -Name "DefaultBrowserSettingEnabled" -Value 0 -Type DWord # 应用默认关联设置 dism /online /export-defaultappassociations:C:\temp\DefaultAssoc.xml $assocContent = Get-Content C:\temp\DefaultAssoc.xml -Raw $assocContent = $assocContent -replace '<Application Name="Microsoft Edge"', '<Application Name="Google Chrome"' $assocContent = $assocContent -replace 'Microsoft.MicrosoftEdge_8wekyb3d8bbwe', 'Google.Chrome' $assocContent | Set-Content C:\temp\ModifiedAssoc.xml -Force dism /online /import-defaultappassociations:C:\temp\ModifiedAssoc.xml # 清理临时文件 Remove-Item C:\temp\DefaultAssoc.xml, C:\temp\ModifiedAssoc.xml -Force -ErrorAction SilentlyContinue Write-Host "Chrome已设置为默认浏览器,部分设置需重启系统生效"
脚本说明
- 路径检查:先验证Chrome安装路径,避免无效操作
- 权限验证:确保脚本以管理员身份运行,否则无法修改HKLM和策略键
- 多维度关联设置:覆盖HTML/HTM文件及HTTP/HTTPS协议的机器级和用户级注册表项
- Edge恢复禁用:通过组策略阻止Edge自动重置默认浏览器
- DISM关联导入:强制系统应用修改后的默认关联配置
注意事项
- 若使用64位Chrome,需将
$chromePath改为"C:\Program Files\Google\Chrome\Application\chrome.exe" - 部分企业环境可能有组策略强制Edge为默认,需先解除相关限制
- 执行后建议重启系统,确保所有设置生效
内容的提问来源于stack exchange,提问作者Anonynous
相关产品推荐
相关产品推荐

