Open ampersand & containing URL with Start Command


👤 Diwas Poudel    🕒 Jun 6 2022    📁 Fix    📜 0 comment

START Command is a helpful command that enables a user to open a separate new window from within the Windows Command line. Its general syntax is:

 START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
      [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
      [/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
      [/MACHINE <x86|amd64|arm|arm64>][command/program] [parameters]

where:

  • title: Sets the Command Prompt window title.
  • /d path: Specifies the startup directory.
  • options:
  • command: Specifies the command or program or web URL to start.
  • parameter: Sets parameters for the command or program and many more. (source)

Open a simple URL with START Command

Using the Start Command, we can easily open the website URL in the following format:

START www.google.com

This will open the website in the default web browser.

Open a URL that contains an ampersand & with the START command

However, if you try to open a website that contains a query string and a special character, it will cause an error. For demonstration, I am trying to access the below URL containing the special character(&) in the string.

START www.google.com/search?q=tricks&source=hp

After you have entered this command into the Command Prompt and pressed Enter, you will find that the website page will give the result up to "https://www.google.com/search?q=tricks" and the remaining &source=hp will give an error with the message that is shown below.

'source' is not recognized as an internal or external command, operable program, or batch file.

Screenshot of the error message:

ampersand-error-in-web-url-start-command
fig. Error while opening web URL having &

In this case, everything that comes after the ampersand (&) will be read as its separate command which will result in an error.

This is due to the fact that the Command Prompt will consider & to be a special character. So in this case everything that comes after the ampersand (&) will be read as its separate command which will result in an error.

Other special characters that can be entered into the Command Prompt include # $ & + ,  / : ; = ? @ [ ] and so on. In the event that you use any of these characters in the command line, it will not treat the string as a normal one. Therefore, the primary focus of our work will be to convert the URL to a normal string.

Ways to open a Website containing an ampersand (&)

There are 3 ways for it. Let's look at each of them in-depth.

Way 1: By adding a caret character ( ^ ) before each & ampersand.

& is a special character in bash; therefore, if the URL contains a special character, you must prefix each & with ^. 

Example:

START www.google.com/search?q=tricks^&source=hp

This denotes that the character is to be treated like any other character, and it takes away any special meaning it may have previously held.

Multiple & ampersands in URL

Take note that if you have multiple & ampersands, you need to add the ^ sign just before each of them as shown below

START your_url?a=querystring1^&b=querystring2^&c=querystring3

Ways 2: Using Empty Quotes before website URL and query string

When using the Start Command, we are required to give the dummy argument as the first argument. You can simply give it an empty string.

Example:

START "" "www.google.com/search?q=tricks&source=hp"

Also do not forget to double-quote the web URL.

Ways 3: Using Powershell to open website URL using double quotes

In Powershell, you do not have to do anything. Just enclose it in double quotes.

Example:

START  "www.google.com/search?q=tricks&source=hp"

In Powershell, there are additional methods for opening web URLs with query strings and ampersands. This method employs the start-process command.

 Start-Process 'https://www.google.com/search?q=tricks&source=hp'

Conclusion:

In this way, we can successfully open a URL that contains an ampersand & with the START command