Skip to content

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:

powershell
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
  1. Visit the PowerShell GitHub releases page
  2. Download the latest version for your system
  3. Install and use PowerShell 7+ instead of Windows PowerShell

After upgrading, your original command will work as expected:

powershell
npm run build && node ./dist/main.js

Solution 2: Use the Semicolon Operator

Replace && with a semicolon (;) to execute commands sequentially:

powershell
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:

powershell
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:

powershell
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:

powershell
(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:

powershell
npm run build
node ./dist/main.js

Practical Examples

Webpack Build Scenario

The original problem from StackOverflow involved building a React project:

powershell
npm run build && node ./dist/main.js
powershell
npm run build; if ($LASTEXITCODE -eq 0) { node ./dist/main.js }
powershell
npm run build && node ./dist/main.js

Git Operations Example

For Git commands that you might chain together:

powershell
git fetch --all && git add --all && git commit -m 'update' && git push
powershell
git fetch --all; if ($?) { git add --all; if ($?) { git commit -m 'update'; if ($?) { git push } } }
powershell
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:

powershell
$PSVersionTable.PSVersion

Look for the Major version number:

  • Version 5.x or below: Use workarounds
  • Version 7.x or above: && works natively

Best Practices

  1. Check your PowerShell version before writing scripts that use &&
  2. Use exit code checking ($LASTEXITCODE -eq 0) for robust conditional execution
  3. Consider upgrading to PowerShell 7+ for better compatibility with cross-platform scripts
  4. 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.