12

In bash I use ./task1.sh && ./task2.sh to run task1, and then run task2 as long as task1 did not return an error code. When I try .\task1.bat && .\task2.bat in Powershell I get the error "The Token && Is not a valid separator in this version."

Is there an equivalent for && in Windows Powershell?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Ryan
  • 9,918
  • 7
  • 42
  • 57
  • it's only proper ettiquette to select an answer when your question has been answered on the Stack Exchange network. please select an answer. thanks! – Dario Russo Feb 07 '14 at 21:56
  • Whoever is interested in Bash-style `&&` and `||` becoming a part of PowerShell: please vote for the feature [here](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11087898-implement-the-and-operators-that-bash-has). – mklement0 Jan 21 '17 at 19:11
  • 2
    You can just do `(command) -and (command)` – leonheess Jan 11 '20 at 09:52

2 Answers2

18

This is the closest I can get:

  .\task1.ps1 ; if($?){.\task2.ps1}
manojlds
  • 290,304
  • 63
  • 469
  • 417
mjolinor
  • 66,130
  • 7
  • 114
  • 135
3

I think the closest equivalent would be:

.\task1.bat;if($lastexitcode -eq 0){.\task2.bat}

Also, Powershell has -and which you might want to try, but it is not really the same.

manojlds
  • 290,304
  • 63
  • 469
  • 417