PowerShell - Error HandlingYou want to log errors with timestamps and error details in a PowerShell script. Which code snippet correctly achieves this?Acatch { Set-Content -Path 'error.log' -Value "$(Get-Date): $_" }Bcatch { Add-Content -Path 'error.log' -Value "$(Get-Date): $_" }Ccatch { Write-Host "$(Get-Date): $_" > 'error.log' }Dcatch { Write-Error "$(Get-Date): $_" }Check Answer
Step-by-Step SolutionSolution:Step 1: Choose correct cmdlet for appending logsAdd-Content appends text; Set-Content overwrites; Write-Host does not write to files.Step 2: Confirm timestamp and error details are includedcatch { Add-Content -Path 'error.log' -Value "$(Get-Date): $_" } correctly formats timestamp and error message and appends to log file.Final Answer:catch { Add-Content -Path 'error.log' -Value "$(Get-Date): $_" } -> Option BQuick Check:Append error with timestamp using Add-Content [OK]Quick Trick: Use Add-Content with Get-Date for timestamped logs [OK]Common Mistakes:Using Set-Content which overwrites logsTrying to redirect Write-Host output to fileUsing Write-Error which does not write to file
Master "Error Handling" in PowerShell9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More PowerShell Quizzes Error Handling - Custom error messages - Quiz 2easy Error Handling - Why error handling prevents script failure - Quiz 9hard Error Handling - Why error handling prevents script failure - Quiz 12easy Error Handling - Terminating vs non-terminating errors - Quiz 8hard Error Handling - Throw statement - Quiz 15hard Functions - Function definition - Quiz 15hard Functions - Parameters - Quiz 9hard Modules and Script Organization - Script modules vs binary modules - Quiz 2easy Regular Expressions - Common regex patterns - Quiz 6medium Regular Expressions - Regex with Select-String - Quiz 7medium