Write-Error

NAME
    Write-Error

SYNOPSIS
    Writes an object to the error stream.

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

DESCRIPTION
    The 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“.

INPUTS
    System.String
        You can pipe a string that contains an error message to Write-Error.

OUTPUTS
    Error object
        Write-Error writes only to the error stream. It does not return any objects.

NOTES

    ————————– EXAMPLE 1 ————————–

    C:\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 2 ————————–

    C:\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 3 ————————–

    C:\PS>Write-Error -Message “Error: Too many input values.” -Category InvalidArgument

    Description
    ———–
    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