How can I make Powershell behave like Bash with its flag set -e
? set -o errexit
makes a Bash script "Exit immediately if a simple command exits with a non-zero status".
I thought I could do this by setting $ErrorActionPreference="Stop"
but this doesn't seem to work. Suppose I have a script a.ps1
$ErrorActionPreference="Stop"
& cmd /c "exit 1"
echo "cmd exited `$LastExitCode=$LastExitCode and `$?=$?"
If I run it
.\a. ; echo "a.ps1 exited `$LastExitCode=$LastExitCode `$?=$?"
To my surprises it prints
cmd exited $LastExitCode=1 and $?=False
a.ps1 exited $LastExitCode=1 $?=True
What's going on?! I expected a.ps1 to exit after the first line, throwing an error and setting $? to False.
Is there any official documentation explaining $ErrorActionPreference? All I found was this post on a blog about coffee.