Challenge - 5 Problems
PowerShell Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What does the $Error variable contain after a failed command?
Consider this PowerShell snippet:
What will be the output of
$Error.Clear() Get-Item nonexistentfile.txt $Error[0]
What will be the output of
$Error[0]?PowerShell
$Error.Clear()
Get-Item nonexistentfile.txt
$Error[0]Attempts:
2 left
💡 Hint
Remember that $Error stores error objects from commands that fail, even after clearing.
✗ Incorrect
The $Error automatic variable holds error records from failed commands. Clearing it empties previous errors, but the next failed command adds a new error record. So $Error[0] contains the latest error object describing the failure.
💻 Command Output
intermediate2:00remaining
How many errors are stored in $Error after multiple failures?
Run this code:
What is the value of
$Error.Clear() Get-Item missing1.txt Get-Item missing2.txt $Error.Count
What is the value of
$Error.Count?PowerShell
$Error.Clear() Get-Item missing1.txt Get-Item missing2.txt $Error.Count
Attempts:
2 left
💡 Hint
Each failed command adds one error record to $Error.
✗ Incorrect
Each failed command appends an error record to $Error. After two failed Get-Item commands, $Error.Count is 2.
📝 Syntax
advanced2:00remaining
Which option correctly clears the $Error variable?
You want to clear all previous errors stored in $Error. Which command is correct?
Attempts:
2 left
💡 Hint
Think about the method that empties the error collection without removing the variable.
✗ Incorrect
The $Error variable is a collection object with a Clear() method that empties it. Assigning $null or using Clear-Variable does not properly clear the collection contents. RemoveAll() is not a valid method.
🔧 Debug
advanced2:00remaining
Why does this code not clear $Error as expected?
Given this code:
What will happen and why?
$Error = $null Get-Item missingfile.txt $Error.Count
What will happen and why?
PowerShell
$Error = $null Get-Item missingfile.txt $Error.Count
Attempts:
2 left
💡 Hint
Consider what happens when you overwrite $Error with $null.
✗ Incorrect
Assigning $null to $Error removes the error collection object. When the next command fails, PowerShell cannot add the error to $Error because it is null, so accessing $Error.Count causes a runtime error.
🚀 Application
expert2:00remaining
How to retrieve the error message text from the latest error in $Error?
You want to get just the error message string from the most recent error stored in $Error. Which command will output only the error message text?
Attempts:
2 left
💡 Hint
The error record contains an Exception object with the message property.
✗ Incorrect
The $Error variable stores error record objects. The actual error message text is in the Exception.Message property. Using $Error[0].Exception.Message returns just the message string. Other options return either the full error record string or null.