powershell script to open web address URL with default browser

After install a website, we used to open the start page in web browser.

There are at least (not 2) 4 ways to open web address URL with default browser in Powershell.

1. Run a exe file with parameter is our url.

How to get exe filepath of default browser? define and use this function (thanks to Ravikanth Chaganti):

Function GET-DefaultBrowserPath {
#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]
return $browserPath
}

call

Get-DefaultBrowserPath

in my computer Powershell console will return D:\Program Files (x86)\Mozilla Firefox\firefox.exe

2. Simple way:

call

start ‘http://www.gurucore.com’

in Powershell.

Thanks Dac Thach, I tested your solutions and it works on Windows 2008 + Powershell 1.0

3. One another is we use Invoke-Expression

to lend the Windows’ CMD hand (like you open Windows Run / cmd / then type start)

 

Invoke-Expression “cmd.exe /C start http://gurucore.com
Invoke-Expression “cmd.exe /C start http://some-thing.com.net/~en/my-site/gurucore.com#thach
Invoke-Expression “cmd.exe /C start /b http://gurucore.com?ab=abb’&’thach=2+g~

4. Use .NET static function

[Diagnostics.Process]::Start(‘http://gurucore.com?a=-b&c=~d#abc’,’arguments‘)

Advertisements

6 thoughts on “powershell script to open web address URL with default browser

  1. Is there any way to do this in a non memory leak automated way? Having IE started for every request and leaving it open does not seem to be the best way in my opinion.

  2. Have you been able to replace the default Windows association for http:// links with a powershell command that will inspect the URL and open IE for some URL patterns and other browser such as Chrome for all others? I’ve tried and while it works from the command line, it does not work from apps such as MS Office when I click on links (setting HKCR\http\shell\open\command for example)

  3. I have not tried yet.
    The main point is app must able to read “which browser” from configuration. Then if yes, we can replace that configuration to point to our script.

    The CMD app reads it “which browser” from HKCR\http\shell\open\command for example, but Office might read from other config (we can see it opens “Windows default browser” for clicking link in Word, Outlook, …). I assume Office respects “Windows default browser” setting.

    With a quick google, it might be HKCU\SOFTWARE\Clients\StartMenuInternet
    https://superuser.com/questions/287313/which-registry-keys-need-to-be-edited-to-change-the-default-browser

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s