how to run multiple commands on success


In bash & CMD you can do rm not-exists && ls to string together multiple commands, each running conditionally only if the previous commands succeeded.

In powershell you can do rm not-exists; ls, but the ls will always run, even when rm fails.

How do I easily replicate the functionality (in one line) that bash & CMD do?


Most errors in Powershell are “Non-terminating” by default, that is, they do not cause your script to cease execution when they are encountered. That’s why ls will be executed even after an error in the rm command.

You can change this behavior in a couple of ways, though. You can change it globally via the $errorActionPreference variable (e.g. $errorActionPreference = 'Stop'), or change it only for a particular command by setting the -ErrorAction parameter, which is common to all cmdlets. This is the approach that makes the most sense for you.

Or, using some common shorthand

The -ErrorAction parameter is explained the help. Type Get-Help about_CommonParameters


how to run multiple commands on success by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply