The ErrorAction
parameter and the ErrorActionPreference
variable are used to control what happens when a non-terminating error is written.
Note
CmdletBinding
:The ErrorAction
parameter is only available if a function declares the CmdletBinding
attribute. CmdletBinding
is automatically added is automatically added if the Parameter
attribute is used.
By default, the ErrorAction
is set to continue. Any non-terminating errors will be displayed, but a script or function will continue to run.
If the ErrorAction
is set to SilentlyContinue
, errors will be added to the automatic variable $error
, but the error will not be displayed.
The following function writes a non-terminating error using Write-Error
:
function Invoke-Something { [CmdletBinding()] param ( ) Write-Error 'Something went wrong' } Invoke-Something-ErrorAction SilentlyContinue
The error is written, but hidden from view. The error may be viewed as the latest entry in the $error
variable:
PS> $Error[0] Invoke...