Writes an object to the error stream. (Write-Error)
NAMEWrite-ErrorSYNOPSISWrites an object to the error stream.SYNTAXWrite-Error -ErrorRecord <ErrorRecord> [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [-RecommendedAction <string>] [<CommonParameters>] Write-Error [-Message] <string> [-Category {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>] [-TargetObject <Object>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [-RecommendedAction <string>] [<CommonParameters>] Write-Error -Exception <Exception> [-Category {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>] [-Message <string>] [-TargetObject <Object>] [-CategoryActivity <string>] [-CategoryReason <string>] [-CategoryTargetName <string>] [-CategoryTargetType <string>] [-RecommendedAction <string>] [<CommonParameters>]DESCRIPTIONThe Write-Error cmdlet writes an error to the Windows PowerShell error stream. By default, errors are sent to the host program to be displayed, along with output. You can write an error by submitting an error message string, an ErrorRecord object, or an Exception object. Use the other parameters of Write-Error to populate the error record.PARAMETERS-Category <ErrorCategory> Specifies the category of the error. The default value is NotSpecified. For information about the error categories, see "ErrorCategory Enumeration" in the MSDN (Microsoft Developer Network) library at http://go.microsoft.com/fwlink/?LinkId=143600. Required? false Position? named Default value NotSpecified Accept pipeline input? false Accept wildcard characters? false -CategoryActivity <string> Describes the action that caused the error. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -CategoryReason <string> Explains how or why the activity caused the error. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -CategoryTargetName <string> Specifies the name of the object that was being processed when the error occurred. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -CategoryTargetType <string> Specifies the .NET type of the object that was being processed when the error occurred. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -ErrorId <string> Specifies an ID string to identify the error. The string should be unique to the error. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -ErrorRecord <ErrorRecord> Specifies an error record object that includes properties that describe the error. To get an error record object, use the New-Object cmdlet or take an error record object from the array in the $Error automatic variable. Required? true Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -Exception <Exception> Specifies an exception that represents the error. You can use an exception instead of specifying message text or an exception record. Required? true Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -Message <string> Specifies 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. Required? true Position? 1 Default value None Accept pipeline input? true (ByValue) Accept wildcard characters? false -RecommendedAction <string> Describes the action that the user should take to resolve or prevent the error. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false -TargetObject <Object> Specifies the object that was being processed when the error occurred. Enter the object (such as a string), a variable that contains the object, or a command that gets the object. Required? false Position? named Default value None Accept pipeline input? false Accept wildcard characters? false <CommonParameters> This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type, "get-help about_commonparameters".INPUTSSystem.String You can pipe a string that contains an error message to Write-Error.OUTPUTSError object Write-Error writes only to the error stream. It does not return any objects.NOTESExamples
EXAMPLE 1C:\PS>get-childitem | foreach-object { if ($_.gettype().tostring() -eq "Microsoft.Win32.RegistryKey") {write-error "Out-of-band object" -errorID B1 -targetobject $_ } else {$_ } }Description----------- This command writes an error when the Get-ChildItem cmdlet returns a Microsoft.Win32.RegistryKey object, such as the objects in the HKLM: or HKCU drives of the Windows PowerShell Registry provider.EXAMPLE 2C:\PS>write-error "Access denied."Description----------- This command writes an "Access denied" error. The command uses the Message parameter to specify the message, but omits the optional Message parameter name.EXAMPLE 3C:\PS>write-error -message "Error: Too many input values." -category InvalidArgumentDescription----------- This command writes a error and specifies an error category. RELATED LINKS Online version: http://go.microsoft.com/fwlink/?LinkID=113425 Write-Debug Write-Verbose Write-Output Write-Host Write-Progress Write-Warning C:\Windows>powershell get-help Write-Output -full
Microsoft Windows [Version 10.0.19045.3693]
Copyright (c) 2023 Microsoft Corporation.
ColorConsole [Version 3.7.1000] PowerShell 2.0-Export