Contact Info

Crumbtrail » Administration » Powershell » Powershell 1.0 » Write-Error

Write-Error - Powershell 1.0 CmdLet

ActiveXperts Network Monitor ships with integrated Powershell scripts to monitor complex network. The scripts run out of the box
Download the ActiveXperts Network Monitor FREE version now »


Write an object to the error pipeline.

Write-Error [-message] string [-exception Exception] [-category ErrorCategory]
             [-errorId string] [-targetObject Object] [-recommendedAction string]
                [-categoryActivity string] [-categoryReason string]
                   [-categoryTargetName string] [-categoryTargetType string]

      Write-Error -errorRecord ErrorRecord [-recommendedAction string]
                [-categoryActivity string] [-categoryReason string]
                   [-categoryTargetName string] [-categoryTargetType string]

-message string
       The message text of the error.

   -exception Exception
       The exception type of the error. This can be used in place of Message and
       ErrorRecord, in which case it should appear as the first parameter.
   -category ErrorCategory
       The category of the error:

   -errorId string
       A unique ID to associate with the error.

   -targetObject Object
       The object with which the error is associated.

   -recommendedAction string
       Describe the recommended response to the error.

   -categoryActivity string
       Describe the action which overrides the ErrorCategoryInfo default.

   -categoryReason string
       Describes the reason the ErrorCategoryInfo default is overridden.

   -categoryTargetName string
       The target name of the ErrorCategoryInfo override.
   -categoryTargetType string
       The target type to override the ErrorCategoryInfo default.
   -errorRecord ErrorRecord
       An error record describing details about the error. This can be used in
       place of Message and Exception, in which case it should appear as the
       first parameter.

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.

$error is an array containing recent errors, $error[0] is the most recent.
$? is a boolean variable for executable status, it will be true if an entire script succeded, if any operations generated an error then $? will be false.


Force a single error using Get-Date and then display the $error[0] variable:

PS C:\>Get-Date | foreach-Object {Write-Error "Demo Error" -errorID T1 -targetobject $_}
PS C:\>$error[0]