powershellgoogle-chromedefault-browser

Windows error while running powershell script to set chrome as default browser


$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.


Solution


  • 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 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:

    #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."