11

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
  • 72,637
  • 105
  • 156
  • 238
Ryan
  • 9,860
  • 7
  • 41
  • 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
  • 275,671
  • 58
  • 453
  • 409
mjolinor
  • 62,812
  • 6
  • 108
  • 130
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
  • 275,671
  • 58
  • 453
  • 409