In Bash I can easily do something like
command1 && command2 || command3
which means to run command1 and if command1 succeeds to run command2 and if command1 fails to run command3.
What’s the equivalent in PowerShell?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What Bash must be doing is interpreting the integer exit code of the commands as true or false when passed to the logical operators. PowerShell doesn’t do this – but a function can be made to wrap the command and create the same behavior:
($? is a bool containing the success of the last exit code)
Given two batch files:
and
…the behavior can be tested:
The logical operators should be evaluated the same way as in Bash. First, set an alias:
Test: