Bird
0
0

You want to log errors with timestamps and error details in a PowerShell script. Which code snippet correctly achieves this?

hard📝 Workflow Q8 of 15
PowerShell - Error Handling
You 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): $_" }
Step-by-Step Solution
Solution:
  1. Step 1: Choose correct cmdlet for appending logs

    Add-Content appends text; Set-Content overwrites; Write-Host does not write to files.
  2. Step 2: Confirm timestamp and error details are included

    catch { Add-Content -Path 'error.log' -Value "$(Get-Date): $_" } correctly formats timestamp and error message and appends to log file.
  3. Final Answer:

    catch { Add-Content -Path 'error.log' -Value "$(Get-Date): $_" } -> Option B
  4. Quick 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 logs
  • Trying to redirect Write-Host output to file
  • Using Write-Error which does not write to file

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes