$timestamp = Get-Date -Format “yyyy-MM-dd HH:mm:ss”
$errorLog = “C:\PowerShell\ErrorLog.txt”
$errorLog = “C:\PowerShell\ErrorLog.txt”
try {
# Potential error-causing operation
Get-Content -Path “C:\NonexistentFile.txt”
}
catch {
# Log error details to a file
$errorMessage = “$timestamp – Error: $($_.Exception.Message)”
Add-Content -Path $errorLog -Value $errorMessage
Write-Host “An error occurred and was logged.”
}
--------------------------------- End --------------------------------------------