Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to display the most recent error stored in the $Error automatic variable.
PowerShell
Write-Output $Error[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .Count instead of an index to access an error.
Using [1] which accesses the second error, not the most recent.
Trying to clear errors instead of displaying them.
✗ Incorrect
The $Error variable stores errors in a collection, with the most recent error at index 0. Using $Error[0] accesses the latest error.
2fill in blank
mediumComplete the code to clear all errors from the $Error automatic variable.
PowerShell
$Error[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .Remove() which requires an argument and removes a specific item.
Using .Reset() or .Delete() which are not valid methods for $Error.
✗ Incorrect
The .Clear() method empties the $Error collection, removing all stored errors.
3fill in blank
hardFix the error in the code to display the error message of the most recent error.
PowerShell
Write-Output $Error[1].Exception.Message Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .Count which is a number, not an error object.
Using [1] which accesses the second error, not the most recent.
Trying to call .Clear() which clears errors instead of displaying.
✗ Incorrect
To get the message of the most recent error, access $Error[0].Exception.Message. Using [0] gets the latest error object.
4fill in blank
hardFill both blanks to check if there are any errors and display the count.
PowerShell
if ($Error[1] -gt 0) { Write-Output "Errors: $($Error[2])" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using [0] or [1] which access specific errors, not the count.
Using .Length which is similar but .Count is preferred for collections.
✗ Incorrect
The .Count property gives the number of errors in $Error. Using it in both places checks if errors exist and shows how many.
5fill in blank
hardFill all three blanks to create a filtered list of error messages containing the word 'timeout'.
PowerShell
$timeoutErrors = $Error | Where-Object { $_.Exception.Message[1] '*timeout*' } | ForEach-Object { $_.Exception.[2] } | Sort-Object [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Exception.Message which is nested and invalid here.
Using -eq instead of -like which looks for exact matches.
Sorting by Exception.Message which is not a direct property.
✗ Incorrect
Use -like to filter messages containing 'timeout'. Extract the Message property from $_.Exception, then sort by Length to order by message size.