PowerShell: "&& is not a valid statement separator in this version"

You pasted a command that works in bash or in Command Prompt — something like <code>npm install && npm test</code> — and Windows PowerShell answers: <i>The token '&&' is not a valid statement separator in this version.</i> That is a version difference, not a broken install.

What the error looks like

PS C:\code\app> npm install && npm test
At line:1 char:13
+ npm install && npm test
+             ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

The same message appears for ||. You are almost certainly in Windows PowerShell 5.1 (powershell.exe), not PowerShell 7 (pwsh.exe).

Check which PowerShell you have

$PSVersionTable.PSVersion
# 5.1.x… → Windows PowerShell (no && / ||)
# 7.x…   → PowerShell 7 (pipeline chain operators work)

Get-Command powershell, pwsh -ErrorAction SilentlyContinue |
  Select-Object Name, Source, Version

Fixes that work in 5.1

Goal5.1-safe pattern
Run B after A, alwaysnpm install; npm test
Run B only if A succeeded (exit 0)npm install; if ($LASTEXITCODE -eq 0) { npm test }
Two native cmds with classic &&cmd /c "npm install && npm test"
Boolean “and” of two PowerShell expressionsif ((Test-Path a) -and (Test-Path b)) { … }
Call an exe whose path has spaces& "C:\Program Files\nodejs\node.exe" -v (single &)
# Separate lines (clearest for scripts)
npm install
if ($LASTEXITCODE -ne 0) { throw "install failed" }
npm test

# One-liner that stops on failure
npm install; if ($LASTEXITCODE -eq 0) { npm test }

Semicolon vs &&: ; always continues. Bash-style && continues only on success. Copying ; into a place that needs “stop on error” will hide a failed install and still run the tests.

PowerShell 7 has real && and ||

Install PowerShell 7 (winget: winget install Microsoft.PowerShell), then open pwsh instead of Windows PowerShell:

pwsh
npm install && npm test   # works on 7+
git status || echo "not a repo"

VS Code / Windows Terminal: set the default profile to PowerShell 7 so new terminals get pwsh. CI images often still ship 5.1 as powershell; pin pwsh explicitly if your scripts use chain operators.

When to use cmd /c

cmd /c "cd /d C:\code\app && npm ci && npm run build"

Escape carefully: the string is parsed by cmd.exe, not by PowerShell. Prefer rewriting to native PowerShell for anything you will maintain.

Do not confuse with the call operator

A single & is the call operator — it runs a command whose name or path is in a string. That has worked since early PowerShell and is unrelated to this error:

& "C:\Program Files\Git\bin\git.exe" status
$npm = "npm.cmd"; & $npm install

Two ampersands (&&) are the pipeline chain operator — 7+ only.

Related Windows gotchas

Related

Running scripts is disabled in PowerShell · curl alias → Invoke-WebRequest · The term 'node' is not recognized · npm ERR! code ENOENT · Permission denied running script · Text diff

Questions

Why does PowerShell say && is not a valid statement separator?

Windows PowerShell 5.1 does not support the && and || pipeline chain operators. Those arrived in PowerShell 7. Use ; , an if ($LASTEXITCODE -eq 0) block, cmd /c, or install pwsh.

What should I use instead of && in PowerShell 5.1?

Use a semicolon to run the next command always, or npm install; if ($LASTEXITCODE -eq 0) { npm test } to stop on failure. Or wrap the classic chain in cmd /c "… && …".

Does PowerShell 7 support &&?

Yes. From PowerShell 7.0 onward, && and || work much like bash. Check with $PSVersionTable.PSVersion and prefer the pwsh executable.

Is & the same as &&?

No. A single & is the call operator (run this string as a command). && is the "run next only if previous succeeded" chain operator and needs PowerShell 7+.

Why does the same command work in Command Prompt?

cmd.exe has always supported && and ||. PowerShell 5.1 is a different language. Opening "Command Prompt" or using cmd /c uses the CMD rules.

Published .

Try it

Related guides

Newest guides