PowerShell '&&' Operator: Fix "Invalid Statement Separator" Error
Problem Overview
When working with PowerShell, particularly on Windows systems, you may encounter the frustrating error:
The token '&&' is not a valid statement separator in this version.
This error typically occurs when trying to use the &&
operator to chain commands together, such as:
npm run build && node ./dist/main.js
Why This Happens
The &&
operator is a standard feature in many shells (like Bash on Linux/macOS and cmd.exe on Windows) that allows conditional command execution - the second command only runs if the first succeeds.
However, Windows PowerShell versions prior to 7.0 do not support the &&
operator. This is because:
- PowerShell uses different syntax for pipeline chaining
- The
&&
token wasn't implemented as a statement separator in earlier versions - PowerShell has its own set of operators for conditional execution
Solutions
Solution 1: Upgrade to PowerShell 7+
PowerShell 7.0 and later versions now support &&
and ||
operators natively. The simplest solution is to upgrade:
How to upgrade PowerShell
- Visit the PowerShell GitHub releases page
- Download the latest version for your system
- Install and use PowerShell 7+ instead of Windows PowerShell
After upgrading, your original command will work as expected:
npm run build && node ./dist/main.js
Solution 2: Use the Semicolon Operator
Replace &&
with a semicolon (;
) to execute commands sequentially:
npm run build; node ./dist/main.js
WARNING
Unlike &&
, the semicolon doesn't check if the first command succeeded. The second command will run regardless of the first command's success or failure.
Solution 3: Use PowerShell's Conditional Syntax
For true conditional execution (run second command only if first succeeds), use this pattern:
npm run build; if ($?) { node ./dist/main.js }
The automatic variable $?
contains $true
if the last command succeeded, $false
otherwise.
Solution 4: Use Exit Code Checking
For more robust conditional execution, check the actual exit code:
npm run build; if ($LASTEXITCODE -eq 0) { node ./dist/main.js }
This approach is more reliable when commands use stderr redirection (2>
).
Solution 5: Use -and Operator (Advanced)
PowerShell's -and
operator can be used with command execution:
(npm run build) -and (node ./dist/main.js)
Solution 6: Switch to a Different Shell
If you prefer to keep using &&
syntax without upgrading PowerShell:
- Use Command Prompt (cmd.exe)
- Use Git Bash (available with Git for Windows)
- Use Windows Subsystem for Linux (WSL)
Solution 7: Run Commands Separately
Simply run the commands one after another:
npm run build
node ./dist/main.js
Practical Examples
Webpack Build Scenario
The original problem from StackOverflow involved building a React project:
npm run build && node ./dist/main.js
npm run build; if ($LASTEXITCODE -eq 0) { node ./dist/main.js }
npm run build && node ./dist/main.js
Git Operations Example
For Git commands that you might chain together:
git fetch --all && git add --all && git commit -m 'update' && git push
git fetch --all; if ($?) { git add --all; if ($?) { git commit -m 'update'; if ($?) { git push } } }
git fetch --all; git add --all; git commit -m 'update'; git push
PowerShell Version Check
To check your PowerShell version and determine which solutions apply to your system:
$PSVersionTable.PSVersion
Look for the Major version number:
- Version 5.x or below: Use workarounds
- Version 7.x or above:
&&
works natively
Best Practices
- Check your PowerShell version before writing scripts that use
&&
- Use exit code checking (
$LASTEXITCODE -eq 0
) for robust conditional execution - Consider upgrading to PowerShell 7+ for better compatibility with cross-platform scripts
- Document your shell requirements in scripts and documentation
Conclusion
The "token '&&' is not a valid statement separator" error occurs in older PowerShell versions that don't support the &&
operator. The solutions range from simple command separation with semicolons to proper conditional execution using PowerShell's native syntax. For new projects, upgrading to PowerShell 7+ provides the best experience and cross-platform compatibility.