-
-
Save KurtDeGreeff/d7eae7a003c6f88b602b74c31d1b77d8 to your computer and use it in GitHub Desktop.
PowerShell function to open a file in your default browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Internet Explorer may not work well with multiple files | |
#Edge does its own thing | |
Function Out-Browser { | |
[cmdletbinding()] | |
Param( | |
[Parameter(Position = 0, Mandatory,ValueFromPipelineByPropertyName)] | |
[Alias("Path")] | |
[string]$Fullname | |
) | |
Begin { | |
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)" | |
Function GetBrowserCmd { | |
$reg = Get-item -path HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice | |
$ProgId = $reg.GetValue("ProgID") | |
if ($progid -match "^Appx\w+") { | |
Write-Warning "Looks like you are running Microsoft Edge" | |
Return "" | |
} | |
else { | |
(get-itemproperty -path "HKLM:\SOFTWARE\Classes\$ProgID\shell\open\command" -name "(default)").'(default)' | |
} | |
} | |
} #begin | |
Process { | |
$filename = (Convert-Path -path $Fullname).Replace('\','/') | |
$uri = "file:///$filename" | |
Write-Verbose "[PROCESS] Opening $uri" | |
$open = GetBrowserCmd | |
if ($open) { | |
$cmd = $open -replace "%1",$uri | |
Write-Verbose "[PROCESS] $cmd" | |
[regex]$rx = '^.*\.exe"' | |
$app = $rx.match($cmd).value | |
$arg = $rx.split($cmd) | where {$_} | |
start-process -FilePath $app -ArgumentList $arg | |
} | |
else { | |
Write-Verbose "[PROCESS] Opening with Microsoft Edge" | |
start microsoft-edge:$uri | |
} | |
} | |
End { | |
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)" | |
} #end | |
} | |
Set-Alias -name ob -value Out-Browser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment