Bird
0
0

What will be the result of this code snippet?

medium📝 Command Output Q5 of 15
PowerShell - Error Handling
What will be the result of this code snippet?
$Error.Clear(); Get-Item 'C:\missing.txt' -ErrorAction SilentlyContinue; $Error.Count
A0
B1
CAn error is thrown
DThe count of all previous errors
Step-by-Step Solution
Solution:
  1. Step 1: Understand the commands

    $Error.Clear() empties the error list. Get-Item tries to get a missing file but uses -ErrorAction SilentlyContinue to suppress errors.
  2. Step 2: Check $Error after command

    Because errors are suppressed, no new error is added. So $Error.Count remains 0.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    -ErrorAction SilentlyContinue prevents error in $Error [OK]
Quick Trick: -ErrorAction SilentlyContinue stops errors adding to $Error [OK]
Common Mistakes:
  • Expecting $Error to have 1 error despite SilentlyContinue
  • Confusing Clear() method with resetting $Error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes