Bird
0
0

What will be the output of this script?

medium📝 Command Output Q13 of 15
PowerShell - Error Handling
What will be the output of this script?
try {
  Get-Item 'C:\nonexistentfile.txt'
} catch {
  Write-Host 'File not found, please check the path.'
}
AGet-Item : Cannot find path 'C:\nonexistentfile.txt' because it does not exist.
BFile not found, please check the path.
CNo output, script runs silently
DSyntax error in script
Step-by-Step Solution
Solution:
  1. Step 1: Understand Get-Item behavior

    Get-Item throws an error if the file does not exist.
  2. Step 2: Check catch block output

    The catch block catches the error and prints the custom message.
  3. Final Answer:

    File not found, please check the path. -> Option B
  4. Quick Check:

    Error caught and custom message shown = C [OK]
Quick Trick: Catch block shows custom message on error [OK]
Common Mistakes:
  • Expecting default error instead of custom message
  • Thinking script runs silently without output
  • Confusing syntax error with runtime error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes