Skip to content
Latchkey

Azure Pipelines "PowerShell exited with code 1"

A PowerShell or PowerShell@2 task ended non-zero. By default the task fails when PowerShell writes to the error stream or $LASTEXITCODE is non-zero, so a thrown cmdlet error or a failing native command fails the step.

What this error means

A PowerShell step fails with ##[error]PowerShell exited with code '1'. after a cmdlet error or a non-zero native command.

Azure DevOps
Get-Content : Cannot find path 'C:\missing.txt' because it does not exist.
##[error]PowerShell exited with code '1'.

Common causes

A cmdlet threw a terminating error

A cmdlet failed (missing file, bad parameter) and, with errorActionPreference at stop or a write to the error stream, the task fails.

A native command returned non-zero

A called executable set $LASTEXITCODE to non-zero; the task treats that as failure.

How to fix it

Read the cmdlet error and fix it

  1. Find the cmdlet or command error printed above the exit line.
  2. Correct the underlying issue (path, parameter, permission).
  3. Re-run; the step passes once PowerShell exits zero.

Control error handling deliberately

Set errorActionPreference and check exit codes explicitly so only real failures fail the task.

azure-pipelines.yml
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    errorActionPreference: 'stop'
    script: |
      if (-not (Test-Path 'config.json')) { throw 'config.json missing' }

How to prevent it

  • Decide errorActionPreference explicitly for each PowerShell task.
  • Check $LASTEXITCODE after native commands you call.
  • Surface the real cmdlet error rather than only the exit code.

Frequently asked questions

What causes ""PowerShell exited with code '1'""?
A cmdlet failed (missing file, bad parameter) and, with errorActionPreference at stop or a write to the error stream, the task fails.
How do I fix "PowerShell exited with code '1'"?
Read the cmdlet error and fix it

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card