CmdID / PowerShell / Cmdlets / Write
Write-Error
Writes an object to the error stream. PowerShell reference
From the PowerShell reference 11 parameters
What it does
Writes an object to the error stream. The Write-Error cmdlet declares a non-terminating error. By default, errors are sent in the error stream to the host program to be displayed, along with output. To write a non-terminating error, enter an error message string, an ErrorRecord object, or an Exception object. Use the other parameters of Write-Error to populate the error record. Non-terminating errors write an error to the error stream, but they don't stop command processing. If a non-terminating error is declared on one item in a collection of input items, the command continues to process the other items in the collection. To declare a terminating error, use the throw keyword. For more information, see about_Throw.
Write-Error [-Message] <string> [-Category <ErrorCategory>] [-ErrorId <string>] [-TargetObject <Object>] [-RecommendedAction <string>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [<CommonParameters>]Write-Error -Exception <Exception> [-Message <string>] [-Category <ErrorCategory>] [-ErrorId <string>] [-TargetObject <Object>] [-RecommendedAction <string>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [<CommonParameters>]Write-Error -ErrorRecord <ErrorRecord> [-RecommendedAction <string>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [<CommonParameters>]
Write an error for RegistryKey object
Get-ChildItem | ForEach-Object {
if ($_.GetType().ToString() -eq "Microsoft.Win32.RegistryKey")
{
Write-Error "Invalid object" -ErrorId B1 -TargetObject $_
}
else
{
$_
}
}IdentifyWrite an error message to the console
Write-Error "Access denied."IdentifyWrite an error to the console and specify the category
Write-Error -Message "Error: Too many input values." -Category InvalidArgumentIdentifyWrite an error using an Exception object
$E = [System.Exception]@{Source="Get-ParameterNames.ps1";HelpLink="https://go.microsoft.com/fwlink/?LinkID=113425"}
Write-Error -Exception $E -Message "Files not found. The $Files location doesn't contain any XML files."IdentifyIts page in the reference: Write-Error.
Parameters
-CategorySpecifies the category of the error. The default value is NotSpecified. The acceptable values for this parameter are:
- 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
- ProtocolError
- ConnectionError
- AuthenticationError
- LimitsExceeded
- QuotaExceeded
- NotEnabled
For information about the error categories, see ErrorCategory Enumeration.-CategoryActivitySpecifies the action that caused the error.-CategoryReasonSpecifies how or why the activity caused the error.-CategoryTargetNameSpecifies the name of the object that was being processed when the error occurred.-CategoryTargetTypeSpecifies the type of the object that was being processed when the error occurred.-ErrorIdSpecifies an ID string to identify the error. The string should be unique to the error.-ErrorRecordSpecifies an error record object that represents the error. Use the properties of the object to describe the error.
To create an error record object, use the New-Object cmdlet or get an error record object from the array in the $Error automatic variable.-ExceptionSpecifies an exception object that represents the error. Use the properties of the object to describe the error.
To create an exception object, use a hash table or use the New-Object cmdlet.-MessageSpecifies the message text of the error. If the text includes spaces or special characters, enclose it in quotation marks. You can also pipe a message string to Write-Error.-RecommendedActionSpecifies the action that the user should take to resolve or prevent the error.-TargetObjectSpecifies the object that was being processed when the error occurred. Enter the object, a variable that contains the object, or a command that gets the object.