Bird
0
0

What will be the output of this PowerShell snippet?

medium📝 Command Output Q13 of 15
PowerShell - Error Handling
What will be the output of this PowerShell snippet?
try {
  Get-Item 'C:\nonexistentfile.txt'
} catch {
  Add-Content error.log "$(Get-Date): File not found error"
  'Error logged'
}
ANo output, only error.log updated
BError logged
CFile not found error
DScript stops with error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try block

    The command tries to get a file that does not exist, causing an error.
  2. Step 2: Analyze catch block

    The catch block appends a timestamped error message to error.log and outputs the string 'Error logged'.
  3. Final Answer:

    Error logged -> Option B
  4. Quick Check:

    Catch outputs message after logging [OK]
Quick Trick: Catch block outputs text after logging error [OK]
Common Mistakes:
  • Thinking no output appears because only file is updated
  • Expecting script to stop on error
  • Confusing error message with output string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes