The $Error variable helps you see what errors happened in your PowerShell session. It keeps a list of recent errors so you can check and fix problems.
$Error automatic variable in PowerShell
Introduction
Syntax
PowerShell
$Error
$Error is an automatic variable that stores error objects in a list.
The newest error is at $Error[0], and older errors have higher indexes.
Examples
PowerShell
Write-Output $Error[0]$Error list.PowerShell
$Error.Clear()
PowerShell
try { Get-Item 'C:\no_such_file.txt' } catch { Write-Output $Error[0] }Sample Program
This script tries to get a file that does not exist. When it fails, it prints a message and shows the error details from $Error[0]. Then it shows how many errors are stored in $Error.
PowerShell
try {
Get-Item 'C:\no_such_file.txt'
} catch {
Write-Output "An error happened."
Write-Output "Error details:"
Write-Output $Error[0]
}
Write-Output "Total errors stored: $($Error.Count)"Important Notes
$Error keeps errors only for the current session or script run.
You can use $ErrorActionPreference to control how PowerShell reacts to errors.
Summary
$Error stores recent errors automatically.
The newest error is $Error[0].
You can clear errors with $Error.Clear().
Practice
1. What does the PowerShell automatic variable
$Error store?easy
Solution
Step 1: Understand the purpose of
$Error$Errorautomatically collects errors that occur during the PowerShell session.Step 2: Differentiate from other variables
It does not store outputs, inputs, or environment variables, only errors.Final Answer:
A list of recent errors encountered during the session -> Option AQuick Check:
$Errorstores errors [OK]
Hint: Remember: $Error always holds recent errors [OK]
Common Mistakes:
- Thinking $Error stores command outputs
- Confusing $Error with environment variables
- Assuming $Error stores user inputs
2. Which of the following is the correct way to clear all errors stored in
$Error?easy
Solution
Step 1: Identify the method to clear errors
$Erroris an automatic variable with a methodClear()to empty its contents.Step 2: Evaluate other options
Assigning $null or removing the variable does not properly clear the error collection;Clear-Erroris not a valid cmdlet.Final Answer:
$Error.Clear() -> Option CQuick Check:
Use$Error.Clear()to clear errors [OK]
Hint: Use $Error.Clear() to empty errors quickly [OK]
Common Mistakes:
- Trying to assign $null to $Error
- Using non-existent cmdlets like Clear-Error
- Removing $Error variable instead of clearing
3. Given this PowerShell code:
What will be the output?
Try { Get-Item 'C:\nonexistentfile.txt' } Catch { Write-Host 'Error caught' }
$latestError = $Error[0]
Write-Output $latestError.Exception.MessageWhat will be the output?
medium
Solution
Step 1: Understand the Try-Catch block behavior
The command tries to get a file that does not exist, causing an error caught by Catch, which writes 'Error caught'.Step 2: Analyze $Error[0] and output
$Error[0] holds the most recent error object. AccessingException.Messageoutputs the detailed error message about the missing file.Final Answer:
Cannot find path 'C:\nonexistentfile.txt' because it does not exist. -> Option BQuick Check:
$Error[0].Exception.Message shows error details [OK]
Hint: Remember $Error[0] is newest error object [OK]
Common Mistakes:
- Confusing Write-Host output with $Error content
- Expecting 'Error caught' from Write-Host as output
- Assuming no error is stored in $Error
4. You run this code:
But the output is empty. What is the likely reason?
Get-Item 'C:\missingfile.txt'
Write-Output $Error[0]
But the output is empty. What is the likely reason?
medium
Solution
Step 1: Check if error occurred
Since the file is missing, Get-Item should cause an error and populate $Error.Step 2: Consider why $Error[0] is empty
If $Error was cleared (e.g., by $Error.Clear()) before accessing, it will be empty despite the error.Final Answer:
The error was cleared before accessing $Error -> Option AQuick Check:
Clearing $Error empties error list [OK]
Hint: Check if $Error was cleared before reading it [OK]
Common Mistakes:
- Assuming $Error is empty if no error occurred
- Confusing $Error with $Errors variable
- Thinking $Error is unavailable in session
5. You want to write a script that tries to get a file, and if it fails, clears the error list and logs the error message only once. Which code snippet correctly uses
$Error to achieve this?hard
Solution
Step 1: Identify correct error handling and clearing order
Try { Get-Item 'C:\file.txt' } Catch { $msg = $Error[0].Exception.Message; $Error.Clear(); Write-Output $msg }captures the error message from $Error[0], then clears the error list, then outputs the message.Step 2: Check other options for mistakes
Try { Get-Item 'C:\file.txt' } Catch { Write-Output $Error.Clear(); Write-Output $Error[0].Exception.Message }clears errors before reading message (wrong order).Get-Item 'C:\file.txt' -ErrorAction SilentlyContinue; Write-Output $Error[0].Exception.Message; $Error.Clear()
does not use Try-Catch and may fail silently.Try { Get-Item 'C:\file.txt' } Catch { $Error.Clear(); Write-Output $Error[0].Exception.Message }clears errors before reading message, losing the error.Final Answer:
Try { Get-Item 'C:\file.txt' } Catch { $msg = $Error[0].Exception.Message; $Error.Clear(); Write-Output $msg } -> Option DQuick Check:
Read error before clearing $Error [OK]
Hint: Always read $Error before calling $Error.Clear() [OK]
Common Mistakes:
- Clearing $Error before reading error message
- Not using Try-Catch for error handling
- Expecting $Error to update immediately without Try-Catch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
