Open URL with default browser via cmd

User preferences are very important nowadays and if you have a shortcut or want to open an URL for the user you might as well check what is the preferred browser that he uses. This information can be easily found in:

 HKCU\SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice

And from this information you can easily create a batch file that will take the URL desired and open it in the user’s preferred browser:

 

@echo off

cls




if x%1x==xx goto END

set zBrowser="%ProgramFiles(x86)%\Internet Explorer\iexplore.exe"




reg.exe QUERY HKCU\SOFTWARE\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice /v ProgID > "%Temp%\defbrowser.txt"

If %ERRORLEVEL%==0 (




findstr.exe /I /C:"EDGE" "%Temp%\defbrowser.txt"

If %ERRORLEVEL%==0 (

if EXIST "%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe" (

set zBrowser="%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"

goto STARTURL

) ELSE (

goto UNKNOWN

)

)




findstr.exe /I /C:"CHROME" "%Temp%\defbrowser.txt"

If %ERRORLEVEL%==0 (

if EXIST "%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe" (

set zBrowser="%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"

goto STARTURL

) ELSE (

goto UNKNOWN

)

)




findstr.exe /I /C:"FIREFOX" "%Temp%\defbrowser.txt"

If %ERRORLEVEL%==0 (

if EXIST "%ProgramFiles%\Mozilla Firefox\firefox.exe" (

set zBrowser="%ProgramFiles%\Mozilla Firefox\firefox.exe"

goto STARTURL

) ELSE (

goto UNKNOWN

)

)

)




:STARTURL

start %zBrowser% %1

goto END




:UNKNOWN

start %zBrowser% %1




:END

exit

This can be used as OpenURL.cmd www.google.ro.

The file can be downloaded here:

Sharing is caring!

Leave a comment

Your email address will not be published.

4 × 1 =