Invoke-URL to open a webpage from PowerShell prompt

by ravikanthchaganti on January 19, 2010
785 Views

Update: @ShayLevy suggested a one-liner @ http://twitter.com/ShayLevy/statuses/7945284388 and that one-liner is (New-Object -com Shell.Application).Open(http://www.microsoft.com). Nice..! I did not think about this. LL: Always look around for better ways :)

Update: The method I had posted earlier is meaningless since we can open a URL using start-Process or just start. Refer to the comments below.

While experimenting with one of my PowerShell libraries, I thought it would be a good idea to be able to open a URL from a cmdlet or script or even command line. However, I did not want to hard code any browser details (path/image name/etc) in the script. The idea is to find out the default browser setting and then invoke the default browser process to open the URL supplied. So, I came up with this simple script that does the job.

Function Invoke-URL {
    param ([string]$url)            

    #Get the default Browser path
    New-PSDrive -Name HKCR -PSProvider registry -Root Hkey_Classes_Root | Out-Null
    $browserPath = ((Get-ItemProperty 'HKCR:\http\shell\open\command').'(default)').Split('"')[1]            

    #Start the browser path with $url as an argument
    Start-Process $browserPath -ArgumentList $url            

}

What we do here is

  • Look at HKEY_CLASSES_ROOT\http\Shell\Open\Command\(default) value to find out what is the default browser path
  • Start a browser process using Start-Process cmdlet

Here is a simple example to demonstrate use of this

Invoke-URL (Get-Command Invoke-Item).helpuri

Above command will open up the HELP URI for Invoke-Item in your default browser. I have tested this with Internet Explorer and Mozilla FireFox on Windows 7. I don’t see a reason why it would fail with other browsers being default browsers.

Related Posts

{ 4 comments… read them below or add one }

stej January 22, 2010 at 8:48 pm

IMHO it’s too complicated, even the Shay’s solution. What works is just this:
Start-Process -FilePath ‘http://www.ravichaganti.com/blog/?p=1034′

ravikanthchaganti January 22, 2010 at 9:38 pm

Yes. It is. There are many alternatives and I did not think about all that before posting this function :)

Bernd Kriszio February 2, 2010 at 9:59 pm

To open an url I usually just use
start
which is an alias to
Start-Process
. I’m using PowerShell V2 of Windows 7.

ravikanthchaganti February 3, 2010 at 8:22 am

Agree. I think I should take this post offline. It was a mistake on my side. I did not experiment with start-process before posting this here

Leave a Comment

Previous post:

Next post: