PowerShell -ErrorAction: Control How Errors Behave

PowerShell -ErrorAction: Control How Errors Behave

PowerShell Tips Editor 4 min read
PowerShell -ErrorAction: Control How Errors Behave

Every PowerShell cmdlet can encounter errors, and how those errors behave is controlled by the -ErrorAction parameter. Stop an error and enter catch. Suppress it silently. Prompt the user to decide. Understanding each value is essential for writing scripts that fail cleanly instead of failing mysteriously. This guide covers all five PowerShell ErrorAction values, the global preference variable, and the important difference between SilentlyContinue and Ignore.

Quick Answer / TL;DR

# Make all errors terminating (required for try/catch to work)
Get-Item 'C:\Missing\file.txt' -ErrorAction Stop

# Suppress errors completely
Get-Item 'C:\Missing\file.txt' -ErrorAction SilentlyContinue

The Five ErrorAction Values

PowerShell 5.1 has five values; PowerShell 7+ adds Break for debugger integration:

  • Continue — (Default) Display the error message, record it in $Error, and keep going.
  • Stop — Convert to a terminating error. Execution halts unless caught with try/catch.
  • SilentlyContinue — Suppress the display. Still records to $Error. Execution continues.
  • Ignore — Suppress display AND does not record to $Error. Execution continues.
  • Inquire — Prompts the user with Yes/No/Suspend before proceeding. Useful in interactive scripts.
# Default (Continue): shows error message, script continues
Get-Item 'C:\Missing.txt'   # Prints red error, keeps running

# Stop: triggers try/catch
try {
    Get-Item 'C:\Missing.txt' -ErrorAction Stop
} catch {
    Write-Output "Caught the error"
}

# SilentlyContinue: hides the error, records it
Get-Item 'C:\Missing.txt' -ErrorAction SilentlyContinue

# Ignore: completely ignores the error
Get-Item 'C:\Missing.txt' -ErrorAction Ignore
Caught the error

Setting Per-Command ErrorAction

Add -ErrorAction to any cmdlet to control its error behavior for that call only:

# Test if a process is running — suppress the "not found" error
$running = Get-Process -Name 'notepad' -ErrorAction SilentlyContinue
if ($running) {
    Write-Output 'Notepad is running'
}

# Skip files you can't access
Get-ChildItem 'C:\' -Recurse -ErrorAction SilentlyContinue

# Stop on first error in a sequence of file copies
try {
    Copy-Item 'C:\Source\file1.txt' 'C:\Dest\' -ErrorAction Stop
    Copy-Item 'C:\Source\file2.txt' 'C:\Dest\' -ErrorAction Stop
    Copy-Item 'C:\Source\file3.txt' 'C:\Dest\' -ErrorAction Stop
    Write-Output 'All files copied'
} catch {
    Write-Error "Copy failed: $($_.Exception.Message)"
}
Notepad is running
All files copied

Global $ErrorActionPreference

Set the default behavior for all cmdlets in the current scope with the preference variable:

# See current default
$ErrorActionPreference

# Make all errors terminating by default (good for scripts)
$ErrorActionPreference = 'Stop'

# Now any error becomes terminating without -ErrorAction Stop
try {
    Get-Item 'C:\Missing.txt'   # Throws because preference is Stop
} catch {
    Write-Output "Caught without -ErrorAction Stop!"
}

# Restore to default when done
$ErrorActionPreference = 'Continue'
Continue
Caught without -ErrorAction Stop!

Setting $ErrorActionPreference = 'Stop' at the top of a script is a common pattern — it ensures no error goes unhandled. Be aware that this affects all cmdlets in scope, including pipeline cmdlets and functions called from your script.

SilentlyContinue vs Ignore Difference

Both suppress the error display, but they differ in $Error collection behavior:

# Clear the error collection
$Error.Clear()

# SilentlyContinue: adds to $Error
Get-Item 'C:\Missing1.txt' -ErrorAction SilentlyContinue
$Error.Count    # 1

$Error.Clear()

# Ignore: does NOT add to $Error
Get-Item 'C:\Missing2.txt' -ErrorAction Ignore
$Error.Count    # 0

# Practical difference: inspection after the fact
Get-Item 'C:\Missing3.txt' -ErrorAction SilentlyContinue
if ($Error.Count -gt 0) {
    Write-Output "Last error: $($Error[0].Exception.Message)"
}
1
0
Last error: Cannot find path 'C:\Missing3.txt' because it does not exist.

Use SilentlyContinue when you might want to inspect the error later via $Error. Use Ignore when you truly don’t care about the error at all and want the cleanest possible execution without any error tracking overhead.

Using Stop to Enable Try-Catch

The most important practical use of -ErrorAction Stop:

# Without Stop — catch never fires for non-terminating errors
try {
    Get-Item 'C:\Missing.txt'   # Non-terminating — doesn't trigger catch
    Write-Output 'Continues here'
} catch {
    Write-Output 'Never runs'
}

# With Stop — catch fires
try {
    Get-Item 'C:\Missing.txt' -ErrorAction Stop
    Write-Output 'Never reaches here'
} catch {
    Write-Output "Caught it: $($_.Exception.Message)"
}
Get-Item: Cannot find path 'C:\Missing.txt' because it does not exist.
Continues here

Caught it: Cannot find path 'C:\Missing.txt' because it does not exist.

Inspecting $Error Collection

# $Error is a circular buffer of the last 256 errors
$Error[0]                          # Most recent error
$Error[0].Exception.Message        # Error message
$Error[0].InvocationInfo.Line      # Which line caused it
$Error[0].CategoryInfo.Category    # Error category

# Clear all recorded errors
$Error.Clear()

# Set max stored errors
$MaximumErrorCount = 50   # Default is 256

Common Errors and Fixes

  • SilentlyContinue still adds to $Error; Ignore does not: If you’re using SilentlyContinue in a loop and then checking $Error.Count, the count grows with every suppressed error. Use Ignore if you need a clean $Error state, or call $Error.Clear() between operations.
  • Global preference affects all commands including built-ins: Setting $ErrorActionPreference = 'Stop' affects every cmdlet, including internal pipeline operations. If a pipeline filter fails, it becomes terminating. Scope the preference change carefully, or restore it after the section that needs strict handling.

Related Cmdlets / See Also

Wrapping Up

-ErrorAction Stop is the key that makes try/catch work for non-terminating cmdlet errors. Use SilentlyContinue when you want to suppress display but may inspect $Error later. Use Ignore when you truly don’t care. Set $ErrorActionPreference = 'Stop' at script scope for strict error handling. Your next step: audit your existing scripts and add -ErrorAction Stop to any cmdlet inside a try block that isn’t currently triggering catch on failure.

Send-Item -To