The token '&&' is not a valid statement separator in this version

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
3 min

banner

# The token '&&' is not a valid statement separator in this version

The error "The token '&&' is not a valid statement separator in this version" occurs because the two ampersand characters && are not a valid statement separator in PowerShell.

There are multiple ways to resolve the issue:

  1. Rerun the command in CMD (Command Prompt) or Git Bash.
  2. Use the ; character to run the command separately.
  3. Simply run the commands one after the other.
  4. Use the -and statement separator.

Here is an example of how the error occurs.

PowerShell
echo "bobby" && echo "hadz"

Running the command in PowerShell produces the following error.

shell
The token '&&' is not a valid statement separator in this version. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : InvalidEndOfLine

the token is not a valid statement separator in this version

One way to solve the error is to rerun the command in CMD (Command Prompt) or Git Bash.

To open CMD (Command Prompt):

  1. Click on the search field and type cmd:

  2. Start the Command Prompt application.

open cmd on windows

cmd
echo "bobby && echo "hadz"

rerun command with ampersand in cmd

To open Git Bash (assuming you have Git installed):

  1. Click on the search field and type git bash.
  2. Start the Git Bash application.

start git bash application on windows

GitBash
echo "bobby && echo "hadz"

run command without error in git bash

# Alternatively, you can run the command with a semicolon separator in PowerShell

Alternatively, you can use a semicolon separator in PowerShell.

PowerShell
echo "bobby" ; echo "hadz"

run powershell command with semicolon separator

The semicolon separates the two commands, just like a semicolon is used to separate statements in programming languages (e.g. JavaScript).

However the semicolon ; in PowerShell results in the unconditional sequencing of commands.

This is not equivalent to && which only executes the right-hand side of the command on the left-hand side succeeds.

The || (or) characters do the inverse - the right-hand side is only executed if the left-hand side fails.

# Using an if statement to implement && in PowerShell

In some cases, you might only want to run the right-hand side if the left-hand side succeeds.

You can do this by using an if statement.

PowerShell
echo "bobby"; if ($?) {echo "hadz"}

implement separator with if statement

The $? variable is a boolean that indicates whether the most recent command has succeeded.

# Running the commands one after the other

If this suits your use case, you could also run the commands one after the other.

The two ampersand && characters are meant to glue the two commands together.

You can split the commands on each usage of the && characters.

PowerShell
echo "bobby" echo "hadz"

run the two commands separately

# Using the -and option when running the PowerShell command

You can also use the -and option instead of && when running the PowerShell command.

PowerShell
echo "bobby" -and echo "hadz"

Notice that the and is prefixed with a hyphen, so it becomes -and.

Note that the && and || operators are available in PowerShell 7 and more recent versions.

&& and || are known as the Pipeline chain operators in PowerShell 7.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.