How to open weburl using Powershell and Command line?


👤 Diwas Poudel    🕒 18 Mar 2023    📁 TECH

You can open a web URL in a web browser right away by pasting the URL string into the address bar and pressing Enter, but if you want to know how to access a web URL in a command line, batch file, or PowerShell script, you've come to the right place.

Today, ourtechroom will show you how to use Powershell and Command to open a URL in a browser.

Note: Make sure that the command is safe to run before using Invoke-Expression or Start Command.

Windows

Windows provides sophisticated command-line tools. Command prompt and PowerShell are two of the most powerful tools available. Let's look at each of them. 

Start Command in Windows Command Prompt

Command Prompt's Start Command is one of the most frequently used internal commands, and it is supported by all versions of Windows. Its primary function is to Open a file, app, URL etc.

It can be used in a batch file or on the command line.

Basic Syntax

START weburl

Start Command is case insensitive. So, start and START are the same things.

For Example:

START https://stackoverflow.com

This will open ourtechroom site in your default browser.

But following a web URL without HTTP, HTTPS, FTP, or www, does not work.

Example:

start stackoverflow.com 
start asp.net 
start duckduckgo.com

This does not work. Because in this case, Windows will treat stackoverflow.com as a folder and will not find the folder on that path and will give an error stating: Windows cannot find 'stackoverflow.com'.Make sure you typed the name correctly, and then try again.

So, note that it should begin with PROTOCAL:/ or www on the URL.So, that Command line will know the user is attempting to open the command line.

If you want to open Url in Chrome then type:

start chrome https://stackoverflow.com

If you want to open URL in Firefox then type:

start firefox https://stackoverflow.com

If you want to open Url in Microsoft Edge then type:

start msedge https://stackoverflow.com

or

start microsoft-edge:https://stackoverflow.com

If you want to run the "Start" command outside command, you have to do it like this.

Basic Syntax:

cmd /c start url

usage:

cmd /c start https://stackoverflow.com

Powershell

Microsoft's Powershell is a strong tool for automating tasks and managing configurations. In Powershell, there are a number of ways to open a URL in a browser, some of which are outlined here.

1 Using Start-Process Command

A new process is created when you call Start-Process, which inherits the current process's Environment Variables.You can use start-process command in both PowerShell and PowerShell script (ps) files.

Basic Syntax:

Start-Process WebUrl 

If you do not specify anything then by default it will open with the default browser.

Example:

Start-Process 'https://www.stackoverflow.com/'

This will open the stackoverflow.com website in new Windows. As I have set Chrome as the default browser so by default it will open the site in Google Chrome.

Start-Process Open in Internet Explorer 

Start-Process -FilePath 'iexplore.exe' -ArgumentList 'https://www.stackoverflow.com/'

Start-Process Open in Chrome Browser

Start-Process chrome "https://www.stackoverflow.com"

Start-Process Open in Firefox 

Start-Process -FilePath firefox -ArgumentList www.stackoverflow.com

Start-Process Open in Edge 

Start-Process -FilePath msedge -ArgumentList www.stackoverflow.com

Alias Using Shorter Powershell

Start chrome '--start-fullscreen "http://www.google.com"'

Using Invoke-Expression command in PowerShell

According to Windows "The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the expression or command. Without Invoke-Expression, a string submitted at the command line is returned (echoed) unchanged".

ie. Invoke-Expression will take the string and then execute that string. The string can be anything that can be executable by Invoke-Expression.

Example:

Invoke-Expression “cmd.exe /C start https://stackoverflow.com”

Alternative:

'cmd.exe /C start https://stackoverflow.com' |Invoke-Expression

Open Multiple URLs using Powershell

You can open multiple URLs with one PowerShell command as shown below:

 'cmd.exe /C start https://stackoverflow.com','cmd.exe /C start https://ourtechroom.com'|Invoke-Expression

Here we are opening StackOverflow and ourtechroom sites in two different tabs in the browser.

Alternatively,

$url1 = 'https://stackoverflow.com/'
$url2 = 'https://ourtechroom.com'
try {
  Start-Process  $url1
  Start-Process  $url2
}
catch {
 Write-Host "Oops, something went wrong!"
}

Alternative 2:

$urls = @("https://stackoverflow.com/","https://www.ourtechroom.com/")

foreach($url in $urls){
    Start-Process $url
}

Alternative 3:

Start-Process -FilePath Chrome -ArgumentList '--new-window https://stackoverflow.com https://ourtechroom.com'

Powershell For IE:

Opening multiple URL in IE using Powershell command as shown below:

$navOpenInBackgroundTab = 0x1000;
$ie = new-object -com InternetExplorer.Application
$ie.Navigate2("https://stackoverflow.com");
$ie.Navigate2("https://ourtechroom.com", $navOpenInBackgroundTab);
$ie.Visible = $true;

What’s the difference between Invoke-Expression and Start-Process?

The start-Process cmdlet returns the results of the process that was run, and you can use it to interpret the results after that. Invoke-Expression is a quick and dirty process that does not return an object as a result of the executed process.

macOS X

You can use the open command to open the URL in MacOS.

Syntax:

open filename_or_url 

Usage

open https://ourtechroom.com

Linux/Unix

In Linux and Unix, you have to install the xdg-open commands installed. After you have installed you can go and run the following command in the terminal.

Basic Syntax:

xdg-open filename_or_URL

Usage

xdg-open 'https://ourtechroom.com'

How do I use the command line to access a website's URL in a new tab within an existing browser window that is already open?

To access the URL in a new tab, you can use the "start" command with the "http://" or "https://" prefix and the URL as an argument. In addition, you can use the "/newtab" or "-new-tab" switch. For instance, to open the URL "https://www.ourtechroom.com" in a new tab of an already open window of Google Chrome, you can execute the command "start chrome.exe https://www.ourtechroom.com /newtab" by entering it into a command prompt. This will accomplish the desired result.

Syntax:

start chrome.exe https://www.ourtechroom.com /newtab

How do I open a web URL in a new tab of an already open browser window using PowerShell?

You can use the Start-Process cmdlet in conjunction with the "-ArgumentList" parameter to send the URL to the browser process that is already operating. This will cause the URL to be passed along as an argument. For instance, if you want to open the URL "https://www.google.com" in a new tab of an already open window of Google Chrome, you can do so by executing the instruction "Start-Process chrome.exe -ArgumentList '--new-tab https://www.ourtechroom.com'." This will cause the URL to be opened in the new tab.

Syntax:

Start-Process chrome.exe -ArgumentList '--new-tab https://www.ourtechroom.com