Bird
0
0

What will be the output of this PowerShell script?

medium📝 Predict Output Q4 of 15
PowerShell - Error Handling
What will be the output of this PowerShell script?
try {
  Get-Item 'C:\nonexistentfile.txt'
  Write-Output 'File found'
} catch {
  Write-Output 'File not found'
}
AFile found
BFile not found
CScript stops with error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze Get-Item command with invalid path

    Get-Item tries to get a file that does not exist, causing an error.
  2. Step 2: Effect of try-catch on error

    The error is caught by catch block, so 'File not found' is printed instead of stopping.
  3. Final Answer:

    File not found -> Option B
  4. Quick Check:

    Error caught = 'File not found' output [OK]
Quick Trick: Catch block runs on error, printing its message [OK]
Common Mistakes:
  • Expecting script to stop on error
  • Thinking 'File found' prints despite error
  • Assuming no output on error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes