PowerShell ErrorAction
Many PowerShell cmdlets have a common parameter ErrorAction, that can be set to one of Continue, Ignore, Inquire, SilentlyContinue, Stop or Suspend
I’ve never really understood what the different between Ignore and SilentlyContinue was until today. I’d ran the following code:
Get-Service 'NonExistantService' -ErrorAction SilentlyContinueand then happened to look in the $Error automatic variable and noticed the following:
Get-Service : Cannot find any service with service name 'NonExistantService'.
At line:1 char:1
+ Get-Service 'NonExistantService' -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (NonExistantService:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Whereas the following does not get added to $Error
Get-Service 'NonExistantService' -ErrorAction IgnoreThat’s the difference! Ignore was added in PowerShell 3.0, and quoting the documentation page “Unlike SilentlyContinue, Ignore doesn’t add the error message to the $Error automatic variable.”
So Ignore is probably a better option for most cases where ignoring the error is fine.