0
0
PowerShellscripting~10 mins

$Error automatic variable in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A[1]
B.Count
C[0]
D.Clear()
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.
2fill in blank
medium

Complete the code to clear all errors from the $Error automatic variable.

PowerShell
$Error[1]()
Drag options to blanks, or click blank then click option'
A.Remove
B.Clear
C.Reset
D.Delete
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.
3fill in blank
hard

Fix 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'
A[0]
B.Count
C[1]
D.Clear()
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.
4fill in blank
hard

Fill 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'
A.Count
B[0]
C.Length
D[1]
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.
5fill in blank
hard

Fill 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'
A-like
BMessage
CException.Message
DLength
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.