Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Sending Files to Your Browser with PowerShell

Posted on November 6, 2017

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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

image

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)'

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:

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:

image

And open the files in Firefox.

image

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Sending Files to Your Browser with PowerShell”

  1. Rob Duncan says:
    November 7, 2017 at 6:37 am

    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.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d