title: How to make script exit on first error occurrence in powershell?
body: <p>I have the function below where i pass in two arrays. I would like the script to exit on the first occurrence of an error.
  I tried different variations but i can't make the script to stop early.</p>
  <p>I have tried to use <code>($LastExitCode -eq 0)</code> it doesn't seem to work, all the scripts still continue running.</p>
  <p>I also tried <code>If ($? -ne &quot;True&quot;)</code> it doesn't work either, all the tests don't run at all.</p>
  <pre><code>function Run-Coverage {
  
      param($openCoverPath,$testExecutorPath,$dllPath,$output)
  
  &amp; $openCoverPath `
  -threshold:100 `
  -register:user `
  -target:$testExecutorPath `
  -targetargs:&quot;$dllPath&quot; `
  -output:$output `
  
  }
  
  function Run-Tests
  {
      param($Dll,$Xmls)
  
      for ($i=0; $i -lt $Dlls.length; $i++) 
      {
          $TestParam = $Dlls[$i]
          $resultParam = $Xmls[$i]
          $dllPath = -join(&quot;`&quot;&quot;,$projectBasePath,$TestParam,&quot; &quot;,&quot;/TestCaseFilter:`&quot;(TestCategory!=RequiresData)`&quot;&quot;,&quot;`&quot;&quot;)
          $output = -join($outputPath,$resultParam)
  
          try
          {
              Run-Coverage -openCoverPath $openCoverPath -testExecutorPath $testExecutorPath -dllPath $dllPath -output $output
          }
          catch
          {            
              Write-Host &quot;Exiting loop&quot;
              break
          }
      }
  }
  </code></pre>