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.