Understanding the $Error Automatic Variable in PowerShell
📖 Scenario: You are managing a PowerShell script that runs several commands. Sometimes, commands fail, and you want to keep track of these errors to understand what went wrong.
🎯 Goal: Learn how to use the $Error automatic variable in PowerShell to capture and display recent errors from your script.
📋 What You'll Learn
Create a variable to hold some commands that may cause errors
Add a configuration variable to limit the number of errors displayed
Run commands and capture errors automatically in $Error
Display the recent errors stored in $Error
💡 Why This Matters
🌍 Real World
In real scripts, tracking errors helps you find and fix problems quickly without stopping the whole script.
💼 Career
Knowing how to use $Error is important for system administrators and automation engineers to handle errors gracefully in PowerShell scripts.
Progress0 / 4 steps
1
Create commands that cause errors
Create a variable called commands that holds an array with these exact strings: 'Get-Item nonexistentfile.txt' and 'Get-Process invalidprocess'.
PowerShell
Hint
Use an array with @() and include the exact command strings inside single quotes.
2
Set the error count limit
Create a variable called errorLimit and set it to 3 to limit how many errors to display from $Error.
PowerShell
Hint
Just assign the number 3 to the variable $errorLimit.
3
Run commands and capture errors
Use a foreach loop with variable cmd to run each command in commands using Invoke-Expression. Set $ErrorActionPreference to 'Continue' before the loop to capture errors in $Error.
PowerShell
Hint
Set $ErrorActionPreference to 'Continue' so errors are recorded but script continues. Use foreach ($cmd in $commands) and run Invoke-Expression $cmd inside.
4
Display recent errors from $Error
Print the first $errorLimit errors from the $Error automatic variable using Write-Output and array slicing $Error[0..($errorLimit - 1)].
PowerShell
Hint
Use Write-Output $Error[0..($errorLimit - 1)] to show recent errors.
Practice
(1/5)
1. What does the PowerShell automatic variable $Error store?
easy
A. A list of recent errors encountered during the session
B. All successful command outputs
C. User input history
D. System environment variables
Solution
Step 1: Understand the purpose of $Error
$Error automatically 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 A
But the output is empty. What is the likely reason?
medium
A. The error was cleared before accessing $Error
B. Get-Item succeeded, so no error was recorded
C. $Error variable is not available in this session
D. You must use $Errors instead of $Error
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 A
Quick 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?