Batch - Default Browser?

Viewed 31607

Is there a way for using a batch file to find the default browser on my computer?

9 Answers

Simply use

start www.google.com

See here

bubble's answer has some caveats.

This is what works in much more cases for me:

start "" explorer "protocol://your.complicated/url?foo=bar"

The only thing you have to escape is a double quote, and you escape it by typing it once more.

Works at least for protocols http://, https:// and file:// (without query string). Doesn't work for ftp:// URLs (it opens them as a network drive).


Fail cases of the accepted answer

First, the webpage has to start with www.

rem Works
start www.google.com

rem FAILS!
start google.com
The system cannot find the file google.com

rem FAILS!
start translate.google.com
The system cannot find the file translate.google.com

This can be fixed by prepending http://

rem Works
start http://google.com

rem Works even for file:// protocol
start file://C:/test/main.html

rem Works - you can even pass QSA
start http://google.com?foo=bar

rem FAILS! outch, you have to escape ampersands
start http://google.com?foo=bar&baz=baz
'baz' is not recognized as an internal or external command, operable program or batch file.

rem FAILS! but QSA only works for http/s URLs; for file:// protocol, it is ignored
start file://D:/Programování/lumix-link/Control.html?foo=bar
rem opens file://D:/Programování/lumix-link/Control.html

One might e.g. think of enclosing the webpage in double quotes (to avoid misinterpretation of some characters). Then it fails and instead tries to open another CMD.exe with the given string as name:

rem FAILS!
start "http://google.com"

Okay, this is not a bug, just a mislead. This is the expected behavior. Let's fix it:

rem Works
start "" "http://google.com"

rem FAILS!
start "" "google.com"
The system cannot find the file google.com.

So we really have to provide an executable that should be launched. You can see the super-long answers here that succeed better or worse in getting the path to the default browser. But what's much simpler and bullet-proof is to use explorer to launch the webpage. But since explorer decides on what app to launch based on the protocol, you have to use the protocol prefix. And that's it!

rem FAILS!
rem start "" explorer "google.com"

rem Works
rem start "" explorer "http://google.com"

rem Works
rem start "" explorer "http://google.com?foo=bar"

rem FAILS! QSA still not supported on file:// URLs
rem start "" explorer "file://C:/test/main.html?foo=bar"

Thank you, @Rob.

@Rob's answer is close, but it still only pulls the ProgId. This one will set the browser name as reported by the ProgId (not always what you expect) in the browser variable, the specific verb used as browserverb, and the cmd path as browsercmd:

setlocal enableDelayedExpansion
:: parse the ProgId
FOR /F "usebackq tokens=1,2,* delims==" %%A IN (`REG QUERY "HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice" /v ProgId 2^>NUL ^| more +2`) DO (
    SET browserstub=%%C
    :: parse for Class information
    FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!" /v AppUserModelId 2^>NUL`) DO SET "browser=%%T"
    FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!\shell" /ve 2^>NUL`) DO SET "browserverb=%%T"
    FOR /F "usebackq tokens=1,2,* delims==" %%R IN (`REG QUERY "HKCR\!browserstub!\shell\!browserverb!\command" /ve 2^>NUL`) DO SET "browsercmd=%%T"
)
:: display results
SET browser

This is the method i am using to open a browser window

start /REALTIME "~\iexplore.exe" "http://192.168.0.1"

Which opens internet explorer at the logon screen of the router, i have tested it on win 7 and win 10 and it works well. What i have gotten from this forum is that opening any website from a batch file or cmd window will use the default browser.

which works well here on win 10

start /REALTIME "" "http://192.168.0.1"

This also works well.

start /ABOVENORMAL /SEPARATE "" "http://192.168.0.1"

which allows the browser priority over other apps using cpu and also runs in a separate memory space which i have found to cause a lot of work for intruders of sorts.

The empty (path) "" need to be there other wise it just opens another cmd window.

Related