Over the course of the last year I've been using markdown files much more, especially as part of the Platyps module. Even though I have a markdown editor and I can also preview files in VS Code, sometimes I want to see the file in my browser which has a markdown viewer plugin. Or I might want to see something else in my browser. I had been pasting the file path and pasting it into the browser, but of course realized I should get smart about this and write a PowerShell function to make this easier. Thus was born Out-Browser. Although it proved a bit trickier than I expected because the registry was involved.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
I could have simply hard coded the path to Firefox (my default browser) in my function but decided to programmatically get the computer's default web browser. I am running the Windows 10 Fall Creator's Update so there's no guarantee that my registry related code will work on Windows 7 or 8.1. Or even previous versions of Windows 10 for that matter. After much digging around and Internet searching I came up with this approach for determining the default browser.
I need to query this registry key.
$reg = Get-item -path HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice
The ProgID points to my default browser.
$ProgId = $reg.GetValue("ProgID")
Now the tricky part. I need to back track this progid to find the command line associated with opening the browser.
(get-itemproperty -path "HKLM:\SOFTWARE\Classes\$ProgID\shell\open\command" -name "(default)").'(default)'
This is useful because, at least with Firefox, this command will open new tabs in the open instance of my browser. Testing with Chrome did the same. Internet Explorer was a bit more unpredictable and Microsoft Edge lived in its own world. In fact with Microsoft Edge, the ProgID value will start with 'Appx' so if that is the case, I open the browser like this:
start microsoft-edge:$uri
Subbing in my adjusted filepath for the uri. One thing about Edge is that it want to launch associated applications instead of opening the file in Edge itself. If Edge is your browser of choice my function probably isn't going to be very useful.
Most of the command expressions include %1 to indicate the path. So all I need to do is convert the file path into something more suited for the browser.
$filename = (Convert-Path -path $Fullname).Replace('\','/') $uri = "file:///$filename"
And then insert it into the expression in place of %1. Technically, I don't have to convert the path as browsers seem to handle a native Windows path just fine. To launch the process I use regular expression to split the command so I can get the .exe part and assume the rest of the command are arguments.
[regex]$rx = '^.*\.exe"' $app = $rx.match($cmd).value $arg = $rx.split($cmd) | where {$_} start-process -FilePath $app -ArgumentList $arg
And that is all there is too it. Now I can run a PowerShell command like this:
And open the files in Firefox.
The function is posted on GitHub.
https://gist.github.com/jdhitsolutions/325ca7ffec66b71c63f473b2e1316364
If you have problems or suggestions, please post an issue on the page.
Hope you have a great week and accomplish a lot with PowerShell.
Hi Jeffery, I would love to see you tackle windows performance and event monitoring – have you checked out the docker ELK stack https://github.com/deviantony/docker-elk – it takes about 2 minutes to stand up. I’m currently trying to write a powershell function that gathers events and performance logs and throwa them at elasticsearch – the results a very impressive so far.