Skip to content

Instantly share code, notes, and snippets.

@gpduck
Created March 7, 2017 18:48
Show Gist options
  • Save gpduck/cd0449ddbe83b2168e96758e185b4110 to your computer and use it in GitHub Desktop.
Save gpduck/cd0449ddbe83b2168e96758e185b4110 to your computer and use it in GitHub Desktop.
PowerShell Errors
$Error.Clear()
Try {
Get-Process fsdks -ErrorAction Stop
} Catch {
Write-Error "Couldn't find the process"
}
#Prints out the error "Couldn't find the process"
#This contains 2 objects, the "Couldn't find the process" error, and the original error from Get-Process
$Error.Count
$Error
#Reset to go again
$Error.Clear()
Try {
Get-Process fsdks -ErrorAction Stop
} Catch {
#Trying to wrap the original error with more relevant detail
Write-Error "Couldn't find the process" -Exception $_.Exception
}
#Prints out the error "Couldn't find the process"
#Now we only have the error from Get-Process. Even though the additional detail error was printed to the screen, it does not get saved in $Error
$Error.Count
$Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment