Skip to content
Latchkey

How to Set PowerShell ErrorActionPreference to Fail on Errors in GitHub Actions

Setting $ErrorActionPreference to Stop turns cmdlet errors into terminating errors that fail the step.

Add $ErrorActionPreference = "Stop" so non-terminating cmdlet errors stop the step. For native executables, also check $LASTEXITCODE, since their nonzero exits do not throw.

Steps

  • Set $ErrorActionPreference = "Stop" at the top of the pwsh block.
  • After a native command, check if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }.
  • Optionally set Set-PSDebug -Trace 1 while debugging a flaky step.

Workflow

.github/workflows/ci.yml
jobs:
  build:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - name: Strict PowerShell
        shell: pwsh
        run: |
          $ErrorActionPreference = "Stop"
          Get-Item ./does-not-exist.txt    # terminating error fails the step
          npm run build
          if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }

Gotchas

  • $ErrorActionPreference = "Stop" only affects cmdlet errors; native exe exit codes still need a $LASTEXITCODE check.
  • GitHub already sets a strict-ish preference for pwsh steps, but setting it explicitly removes ambiguity.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →