Bird
0
0

Identify the error in this script:

medium📝 Debug Q6 of 15
PowerShell - Error Handling
Identify the error in this script:
$Error = $null; Get-Item 'C:\nofile.txt'; Write-Output $Error[0]
AAssigning $null to $Error breaks its array behavior
BGet-Item command is missing -ErrorAction parameter
CWrite-Output cannot access $Error[0]
DNo error, script runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Analyze $Error assignment

    Assigning $null to $Error removes its array structure, causing errors when accessing $Error[0].
  2. Step 2: Understand impact on later code

    Write-Output $Error[0] will fail because $Error is no longer an array but null.
  3. Final Answer:

    Assigning $null to $Error breaks its array behavior -> Option A
  4. Quick Check:

    Don't assign $null to $Error; reset with @() [OK]
Quick Trick: Reset $Error with @(), not $null, to keep array behavior [OK]
Common Mistakes:
  • Setting $Error to $null instead of empty array
  • Expecting $Error[0] to work after $null assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes