Write an object to the error pipeline. Write error messages along with other information such as an id, object data, and suggested actions.

      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:

         NotSpecified, OpenError, CloseError, DeviceError, DeadlockDetected,
         InvalidArgument, InvalidData, InvalidOperation, InvalidResult, InvalidType,
         MetadataError, NotImplemented, NotInstalled, ObjectNotFound, OperationStopped,
         OperationTimeout, SyntaxError, ParserError, PermissionDenied, ResourceBusy,
         ResourceExists, ResourceUnavailable, ReadError, WriteError, FromStdErr, SecurityError

   -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, -WarningAction, -WarningVariable,
       -OutBuffer -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]

“Knowledge rests not upon truth alone, but upon error also” ~ Carl Gustav Jung


Write-Debug - Write a debug message to the host display
Write-Host - Display objects through the host user interface
Write-Output - Write an object to the pipeline
Write-Progress - Display a progress bar
Write-Verbose - Write a string to the host’s verbose display
Write-Warning - Write a warning message

Copyright © SS64.com 1999-2017
Some rights reserved