Bird
0
0

What is wrong with this error logging approach?

medium📝 Troubleshoot Q7 of 15
PowerShell - Error Handling
What is wrong with this error logging approach?
try { Remove-Item 'C:\file.txt' } catch { Write-Output "Error: $_" > 'error.log' }
AWrite-Output cannot be used inside catch blocks
BThe redirection operator overwrites the log file each time
CRemove-Item does not throw errors
DThe catch block syntax is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand output redirection in PowerShell

    Using > overwrites the file, losing previous logs.
  2. Step 2: Identify better approach

    Appending with >> preserves existing logs; Write-Output works in catch; Remove-Item throws errors.
  3. Final Answer:

    The redirection operator overwrites the log file each time -> Option B
  4. Quick Check:

    Use >> to append logs, not > [OK]
Quick Trick: Use >> to append logs, not > which overwrites [OK]
Common Mistakes:
  • Using > which overwrites log files
  • Thinking Write-Output is invalid in catch
  • Assuming Remove-Item never errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes