Default Browser - set url association with PowerShell

 Something that is helpful to know for complicated environments.

Background:

We have a legacy software that is in the process of being rewritten as a Blazor application, as we begin integrating these things together one need is to make sure that invoked urls do not open in Internet Explorer. 

Policy and OU based controls are not available within the timeline we're looking at. However we have an advantage in the fact that we're already using a .ps1 script that prepares some environment needs before  the software executes.


Other alternatives:

A popular recommendation for handling this is to work within the DefaultAssociationsConfiguration structure. This is a good option, and is well covered in articles like this: set-default-browser-to-microsoft-edge-using-powershell/


Why this solution:

This solution fits a need where anything that may cause IE to be reset as default is handled promptly without requiring a logoff or restart to take effect. 

Components:

Registry values:

  • HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
  • HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice

PowerShell Registry Tools:

Get a list of protocol associations on a system:

get-item HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations | gci


Get the property value of a specific item:

$regItem = Get-item HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
$regItem | Get-ItemProperty  -Name ProgId


Set the property value of a specific item:

$regItem = Get-item HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
$regItem | Set-ItemProperty  -Name ProgId -Value ChromeHTML


List of potentially useful ProgID values:

IE.HTTPInternet Explorer
ChromeHTMLChrome
MSEdgeHTMEdge


Testing:

Testing showed this change to be effective, but it does involve a user confirmation step. 

The next time the protocol was invoked the user was prompted with "How would you like to open?" which is sufficient for our environment. 

For another environment it might be necessary to find the properties needed to control this without user confirmation. (If anyone does that research I'd be interested to hear about it.)

Note: by "protocol was invoked" I specifically mean execution invoked via explorer (think of using a .lnk file, or clicking a link in outlook). When you already have a browser open, and click a link inside that browser it handles it directly without asking explorer what the shell association is.


Implementation:

Here is the code we've added to an existing PowerShell script:

# Make sure that IE is not the default browser for this user.
$DefaultBrowser = ( Get-ItemProperty
            HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
             -Name ProgId
            ).ProgId

if ($DefaultBrowser -eq 'IE.HTTP')
{
    # Prevent IE from being default by changing the Program ID value to Chrome.
    # The next time a url is clicked, it prompts the user with "Choose how to open"
    Set-ItemProperty
            HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice\
             -Name ProgId -Value ChromeHTML
    Set-ItemProperty
            HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice
             -Name ProgId -Value ChromeHTML
}


Comments

Popular posts from this blog

LINQ within a foreach()

Rules of Automation