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.
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
- Find the cmdlet or command error printed above the exit line.
- Correct the underlying issue (path, parameter, permission).
- 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.
- task: PowerShell@2
inputs:
targetType: 'inline'
errorActionPreference: 'stop'
script: |
if (-not (Test-Path 'config.json')) { throw 'config.json missing' }How to prevent it
- Decide
errorActionPreferenceexplicitly for each PowerShell task. - Check
$LASTEXITCODEafter native commands you call. - Surface the real cmdlet error rather than only the exit code.