title: How can I get powershell to return the correct exit code when called with the -File argument?
body: <p>Powershell is returning a 0 exit code, when an error has occurred, if called with the -File argument. Which means my build is green when it shouldn't be :(</p>
  
  <p>For example:</p>
  
  <p>(in wtf.ps1)  </p>
  
  <pre><code>$ErrorActionPreference = "Stop";   
  $null.split()
  </code></pre>
  
  <p>(cmd)  </p>
  
  <pre><code>powershell -file c:\wtf.ps1  
  You cannot call a method on a null-valued expression.
  At C:\wtf.ps1:3 char:12
  + $null.split &lt;&lt;&lt;&lt; ()
      + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
     insErrorRecordException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  
  echo %errorlevel%  
  0
  
  powershell c:\wtf.ps1  
  You cannot call a method on a null-valued expression.
  At C:\wtf.ps1:3 char:12
  + $null.split &lt;&lt;&lt;&lt; ()
      + CategoryInfo          : InvalidOperation: (split:String) [], ParentConta
     insErrorRecordException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  
  echo %errorlevel%  
  1
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>(I've tried pretty much every idea from the first 2 pages of this: <a href="https://www.google.co.uk/search?q=powershell+file+argument+exit+code" rel="noreferrer">https://www.google.co.uk/search?q=powershell+file+argument+exit+code</a> already)</p>