$browserPath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
$browserKey = "HKLM:\SOFTWARE\Classes\ChromeHTML\shell\open\command\"
Set-ItemProperty -Path $browserKey -Name "(Default)" -Value $browserPath
This PowerShell script successfully ran on Windows 10 22H2 for your colleague but failed to work when you tried it on a VMware environment with same version.
Despite attempting various scripts, I encountered errors that caused the default browser to revert back to Microsoft Edge (MSEDGE). I am seeking assistance in creating a PowerShell script that can effectively function on Windows versions ranging from Windows 10 to Windows 11.
The registry key you're targeting would not be sufficient to change a user's default web browser.
Microsoft is trying to prevent the ability to programmatically change the default web browser, by not disclosing a hash algorithm that is used to validate the relevant registry settings.
However, a third-party utility, SetDefaultBrowser.exe
, has figured out this algorithm, and therefore allows fully automated changes to the user's default web browsers:
For background information and a download link, see this blog post.
Alternatively, as you've discovered, if you have Chocolatey installed, you can install SetDefaultBrowser.exe
as follows:
choco install setdefaultbrowser -y
If downloading a third-party utility is not an option, the next best - but suboptimal - solution is to use GUI scripting:
Note that GUI scripting is (a) not robust and limited in terms of what environments it can run from and (b) can break over time, if the GUI changes.
Assuming you have downloaded SetDefaultBrowser.exe
and have placed in a directory listed in your $env:PATH
environment variable, you can make Google Chrome your default browser as follows:
SetDefaultBrowser chrome
The above uses a built-in shortcut for Google Chrome; other available shortcuts as of v1.5: edge
, ie
, iexplore
The above is the equivalent of the following:
SetDefaultBrowser HKLM 'Google Chrome'
HKLM
(HKEY_LOCAL_MACHINE
) indicates the registry hive in which information about the targeted browser is stored; the other possible value is HKCU
(HKEY_CURRENT_USER
)
Running SetDefaultBrowser
without arguments will show you what browsers are locally installed, and will list them by registry hive (e.g. HKLM
) and display name (e.g. Google Chrome
(as well as full executable path), which are the two pieces of information you need to pass to make that browser the default
E.g., to make a user-level installation of the Brave browser the default (the alphanumeric suffix may vary, but can be gleaned from the output of an argument-less call):
SetDefaultBrowser.exe HKCU Brave.4E65X2HYVK4P2KKNWFWGNPUBYY
The following is a self-contained solution that installs both Chocolatey and SetDefaultBrowser.exe
on demand, and then changes the default browser to Google Chrome.
Note:
SetDefaultBrowser.exe
itself, once installed, does not require elevation.#Requires -RunAsAdministrator
# Set this to $false to silence the Write-Verbose calls below.
$verbose = $true
# Install SetDefaultBrowswer.exe, if necessary.
if (-not (Get-Command -ErrorAction Ignore SetDefaultBrowser.exe)) {
# Install Chocolatey first, if necessary.
if (-not (Get-Command -ErrorAction Ignore choco.exe)) {
Write-Verbose -Verbose:$verbose "Installing Chocolatey (this will take a while)..."
# Note: We silence success output, Write-Host and Write-Warning messages. The progress display from the Expand-Archive
# call can only be silenced if $ProgressPreference is *globally* set
$prevProgressPref = $global:ProgressPreference; $global:ProgressPreference = 'SilentlyContinue'
try {
Invoke-RestMethod -ErrorAction Stop https://chocolatey.org/install.ps1 | Invoke-Expression >$null 3>$null 6>$null
if (-not (Get-Command -ErrorAction Ignore choco.exe)) { throw "Installation of Chocolatey failed." }
} finally {
$global:ProgressPreference = $prevProgressPref
}
}
Write-Verbose -Verbose:$verbose "Installing SetDefaultBrowser..."
# Note: Place the -y (for automated installation) *at the end* of the command line.
choco install SetDefaultBrowser -y >$null
if ($LASTEXITCODE) { Write-Error "Installation of SetDefaultBrowser failed."; exit $LASTEXITCODE }
# Refresh the environment so that SetDefaultBrowser.exe can be called by name only.
# Do so via Chocolatey's Update-SessionEnvironment cmdlet, which is part of the Chocolatey profile module.
$env:ChocolateyInstall = Convert-Path "$((Get-Command choco.exe).Path)\..\.."
Import-Module -ErrorAction Stop "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
Update-SessionEnvironment
}
Write-Verbose -Verbose:$verbose "Calling SetDefaultBrowser.exe ..."
# Switch to Google Chrome as the default browser.
SetDefaultBrowser chrome
if ($LASTEXITCODE) { exit $LASTEXITCODE }
Write-Verbose -Verbose:$verbose "The user's default browser was successfully changed to Google Chrome."