1

To execute multiple commands in PowerShell we use ; . But there is a flaw in it it executes all the commands even if one fails in between.

Is there any substitute for && in Windows Powershell ?

1

1 Answer 1

2

But there is a flaw in it it executes all the commands even if one fails in between.

Try setting the appropriate preference variable - it defaults to Continue in powershell.exe:

$ErrorActionPreference = 'Stop'

Some-Command
Some-CommandThatMightFail
Some-OtherCommand

Now, if Some-CommandThatMightFail throws an error, execution of the containing script or function will stop and return to the caller immediately.

See the about_Preference_Variables topic in the documentation on more information about controlling error handling behavior with $ErrorActionPreference


Beware that the $ErrorActionPreference = 'Stop' assignment only affects the current scope - overwriting it will only affect the error action preference in the script/function/scriptblock where that assignment was executed:

PS C:\> $ErrorActionPreference
Continue
PS C:\> &{ 
  $ErrorActionPreference = 'Stop'
  # preference only changes inside this scriptblock
  $ErrorActionPreference
}
Stop
PS C:\> $ErrorActionPreference # still Continue in parent scope
Continue
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.