Contact Info

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

Write-Error - Powershell 1.0 CmdLet

Microsoft Windows PowerShell is a command-line shell and scripting tool based on the Microsoft .NET Framework. It is designed for system administrators, engineers and developers to control and automate the administration of Windows and applications.

More than hundred command-line tools (so called "cmdlets") can be used to perform system administration tasks and Windows Management Instrumentation (WMI). These cmdlets are easy to use, with standard naming conventions and common parameters, and standard tools for piping, sorting, filtering, and formatting data and objects.


Write an object to the error pipeline.


-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]