Bird
0
0

What will be the output of this PowerShell code?

medium📝 Command Output Q4 of 15
PowerShell - Error Handling
What will be the output of this PowerShell code?
try { Get-Item 'C:\missing.txt' } catch { Add-Content -Path 'error.log' -Value "Error: $_"; 'Error logged' }
AThe script crashes with an error
BNo output, error is suppressed
CError logged
DThe content of 'missing.txt' is displayed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze try-catch behavior

    Get-Item fails, triggering catch block which logs error and returns 'Error logged'.
  2. Step 2: Understand output from catch block

    The catch block outputs the string 'Error logged' after writing to the log file.
  3. Final Answer:

    Error logged -> Option C
  4. Quick Check:

    Catch block output = 'Error logged' [OK]
Quick Trick: Catch block output is returned after logging error [OK]
Common Mistakes:
  • Expecting no output when error is caught
  • Thinking script crashes despite catch
  • Assuming file content is shown

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes