Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell – Open URL In Windows Default Browser

Posted on January 2, 2020 By David Kittell

As I have found and lost this script from jkdba.com a few times I’m posting it here so I can remember it :)

function Invoke-URLInDefaultBrowser
{
    <#
        .SYNOPSIS
            Cmdlet to open a URL in the User's default browser.
        .DESCRIPTION
            Cmdlet to open a URL in the User's default browser.
        .PARAMETER URL
            Specify the URL to be Opened.
        .EXAMPLE
            PS> Invoke-URLInDefaultBrowser -URL 'http://jkdba.com'
            
            This will open the website "jkdba.com" in the user's default browser.
        .NOTES
            This cmdlet has only been test on Windows 10, using edge, chrome, and firefox as default browsers.
    #>
    [CmdletBinding()]
    param
    (
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]
        [ValidateNotNullOrEmpty()]
        [String] $URL
    )
    #Verify Format. Do not want to assume http or https so throw warning.
    if( $URL -notmatch "http://*" -and $URL -notmatch "https://*")
    {
        Write-Warning -Message "The URL Specified is formatted incorrectly: ($URL)" 
        Write-Warning -Message "Please make sure to include the URL Protocol (http:// or https://)"
        break;
    }
    #Replace spaces with encoded space
    $URL = $URL -replace ' ','%20'
    
    #Get Default browser
    $DefaultSettingPath = 'HKCU:\SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice'
    $DefaultBrowserName = (Get-Item $DefaultSettingPath | Get-ItemProperty).ProgId
    
    #Handle for Edge
    ##edge will no open with the specified shell open command in the HKCR.
    if($DefaultBrowserName -eq 'AppXq0fevzme2pys62n3e0fbqa7peapykr8v')
    {
        #Open url in edge
        start Microsoft-edge:$URL 
    }
    else
    {
        try
        {
            #Create PSDrive to HKEY_CLASSES_ROOT
            $null = New-PSDrive -PSProvider registry -Root 'HKEY_CLASSES_ROOT' -Name 'HKCR'
            #Get the default browser executable command/path
            $DefaultBrowserOpenCommand = (Get-Item "HKCR:\$DefaultBrowserName\shell\open\command" | Get-ItemProperty).'(default)'
            $DefaultBrowserPath = [regex]::Match($DefaultBrowserOpenCommand,'\".+?\"')
            #Open URL in browser
            Start-Process -FilePath $DefaultBrowserPath -ArgumentList $URL   
        }
        catch
        {
            Throw $_.Exception
        }
        finally
        {
            #Clean up PSDrive for 'HKEY_CLASSES_ROOT
            Remove-PSDrive -Name 'HKCR'
        }
    }
}
Invoke-URLInDefaultBrowser "https://kittell.net" # This would load my page in the default browser
Invoke-URLInDefaultBrowser "kittell.net" # This would provide a warning text specifically requesting http:// or https:// to be added to the URL

Reference: http://www.jkdba.com/powershell-open-url-in-default-browser/

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code PowerShell Windows Windows Reg Windows Server

Post navigation

Previous post
Next post

Related Posts

Exporting the Schema from the Source Forest

Posted on July 25, 2013October 26, 2015

The LDIFDE command-line tool, which ships with Windows Server 2003 and Windows Server 2008, can be used to export the schema from the source forest. This tool creates a file that is formatted with the LDAP Data Interchange Format (LDIF). No special permissions are required to export the schema from…

Read More

ListUserGroup

Posted on February 21, 2013October 26, 2015

public static string ListUserGroup(int userID) { string UsersGroups = ""; if (userID != 0) { Ektron.Cms.API.Common CommonApi = new Ektron.Cms.API.Common(); Ektron.Cms.API.User.User UserAPI = new Ektron.Cms.API.User.User(); Ektron.Cms.GroupData GroupData = new Ektron.Cms.GroupData(); //Number of Provider(" + System.Convert.ToString(UserAPI.GetUserGroupByName("Provider").GroupId) + ") Users: " + System.Convert.ToString(UserAPI.GetUserGroupByName("Provider").UserCount); Ektron.Cms.GroupData[] gData = UserAPI.GetGroupsUserIsIn(UserAPI.UserId, "GroupID"); foreach (Ektron.Cms.GroupData g in…

Read More

Calculate Age Function

Posted on February 25, 2013October 26, 2015

Two variations of the function but the purpose is to determine the difference in dates and display the result in one column. IF OBJECT_ID(N’dbo.fnAge’) IS NOT NULL DROP FUNCTION dbo.fnAge GO CREATE FUNCTION [dbo].[fnAge] ( @dayOfBirth DATETIME ,@today DATETIME ) RETURNS VARCHAR(100) AS BEGIN DECLARE @thisYearBirthDay DATETIME DECLARE @years INT…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes