title: Powershell analogue of Bash's `set -e`
body: <p>How can I make Powershell behave like Bash with its flag <code>set -e</code>? <code>set -o errexit</code> makes a Bash script "Exit immediately if a simple command exits with a non-zero status". </p>
  
  <p>I thought I could do this by setting <code>$ErrorActionPreference="Stop"</code> but this doesn't seem to work. Suppose I have a script <code>a.ps1</code></p>
  
  <pre><code>$ErrorActionPreference="Stop"
  &amp; cmd /c "exit 1"
  echo "cmd exited `$LastExitCode=$LastExitCode and `$?=$?"
  </code></pre>
  
  <p>If I run it</p>
  
  <pre><code>.\a. ; echo "a.ps1 exited `$LastExitCode=$LastExitCode `$?=$?"
  </code></pre>
  
  <p>To my surprises it prints</p>
  
  <pre><code>cmd exited $LastExitCode=1 and $?=False
  a.ps1 exited $LastExitCode=1 $?=True
  </code></pre>
  
  <p>What's going on?! I expected a.ps1 to exit after the first line, throwing an error and setting $? to False.</p>
  
  <p>Is there any official documentation explaining $ErrorActionPreference? All I found was <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/08/hey-scripting-guy-march-8-2010.aspx" rel="noreferrer">this post</a> on a blog about coffee.</p>