Sending Files to Your Browser with PowerShell

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.

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.

image

The ProgID points to my default browser.

Now the tricky part. I need to back track this progid to find the command line associated with opening the browser.

image

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:

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.

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.

And that is all there is too it. Now I can run a PowerShell command like this:

image

And open the files in Firefox.

image

The function is posted on GitHub.

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.

One thought on “Sending Files to Your Browser with PowerShell

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *