Bird
0
0

What will be the output of this PowerShell script?

medium📝 Command Output Q4 of 15
PowerShell - Error Handling
What will be the output of this PowerShell script?
try {
  Get-ChildItem -Path 'Z:\nonexistentfolder' -ErrorAction Stop
} catch {
  Write-Host "Error accessing folder: $_"
}
AScript throws an unhandled exception.
BNo output, script runs silently.
CError: Access denied to folder.
DError accessing folder: Cannot find path 'Z:\nonexistentfolder' because it does not exist.
Step-by-Step Solution
Solution:
  1. Step 1: Get-ChildItem with -ErrorAction Stop

    This forces the cmdlet to throw terminating errors.
  2. Step 2: catch block captures error

    The catch block outputs a custom message including the error details ($_).
  3. Final Answer:

    Error accessing folder: Cannot find path 'Z:\nonexistentfolder' because it does not exist. -> Option D
  4. Quick Check:

    Catch block outputs error message with $_ [OK]
Quick Trick: Use -ErrorAction Stop to trigger catch block [OK]
Common Mistakes:
  • Assuming no output when error occurs
  • Confusing access denied with file not found errors
  • Not using -ErrorAction Stop to catch errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes