PowerShell ErrorRecord
An error in PowerShell is an object, not a string. Knowing its shape turns error handling from guesswork into inspection.
Documentation: Microsoft Learn
Overview
The useful properties are $_.Exception.Message for the human-readable text, $_.CategoryInfo for the classification, $_.FullyQualifiedErrorId for a stable identifier you can branch on, $_.TargetObject for the thing that failed, and $_.ScriptStackTrace for where it happened.
FullyQualifiedErrorId is the one worth remembering. It does not change with locale or wording, so comparing against it keeps working when message text does not. Message strings get reworded between versions and are translated on non-English systems.
$Error is an automatic array of recent errors, newest first, so $Error[0] is the most recent one. It is the fastest way to inspect a failure you did not plan for: run the command, then poke at $Error[0] until you understand it.
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
try {
Get-Item 'C:\NoSuchFile.txt' -ErrorAction Stop }
catch {
[pscustomobject]@{
Message = $_.Exception.Message
Category = $_.CategoryInfo.Category
ErrorId = $_.FullyQualifiedErrorId
} }- Stage 1try
Positional values or no named parameters
What happens
-ErrorAction Stop makes the missing-file error terminating so catch receives it. The catch block builds an object from three properties of the ErrorRecord rather than printing a string, so you can see the classification and the error id next to the message. That error id is what you would use in an if statement to handle this specific failure.
Worth remembering
- Errors are ErrorRecord objects with inspectable properties.
- Branch on $_.FullyQualifiedErrorId, not on message text.
- $Error[0] is the most recent error in the session.
- $_.ScriptStackTrace tells you where it came from.
Primary documentation
Try it
Illustrative output · values vary by environment
Example reader
Inspect this page's example
Run supported commands against the practice data, or inspect the example without executing it.
Ready to inspect.
Inspects three properties of the ErrorRecord instead of printing a string.
Show reference output
Illustrative output · values vary by environment
Message Category ErrorId ------- -------- ------- Sample error message InvalidOperation SampleErrorId
Practise this
Handle an explicit failure
Throw the message Validation failed inside try. Catch it and return Input rejected.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
Keep going
Next: PowerShell Verbose and diagnostic streamsCheck your understanding
A catch block contains several error fields. Which ones help distinguish the failing operation?
Inspect Exception.Message, FullyQualifiedErrorId, CategoryInfo and InvocationInfo. Preserve the original ErrorRecord when logging or rethrowing.