PowerShell Logo Small

Write-Error



This is the built-in help made by Microsoft for the command 'Write-Error', in PowerShell version 4 - as retrieved from Windows version 'Microsoft Windows 8.1 Enterprise' PowerShell help files on 2016-06-23.

For PowerShell version 3 and up, where you have Update-Help, this command was run just before creating the web pages from the help files.

SYNOPSIS

Writes an object to the error stream.

SYNTAX


Write-Error [-Message] <String> [-Category <ErrorCategory>] [-CategoryActivity <String>] [-CategoryReason <String>] [-CategoryTargetName <String>] [-CategoryTargetTyp
e <String>] [-ErrorId <String>] [-RecommendedAction <String>] [-TargetObject <Object>] [<CommonParameters>]
Write-Error [-Category <ErrorCategory>] [-CategoryActivity <String>] [-CategoryReason <String>] [-CategoryTargetName <String>] [-CategoryTargetType <String>] [-ErrorI
d <String>] [-Message <String>] [-RecommendedAction <String>] [-TargetObject <Object>] -Exception <Exception> [<CommonParameters>]
Write-Error [-CategoryActivity <String>] [-CategoryReason <String>] [-CategoryTargetName <String>] [-CategoryTargetType <String>] [-RecommendedAction <String>] -Error
Record <ErrorRecord> [<CommonParameters>]



Search powershellhelp.space

DESCRIPTION


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 t
he error record.


Non-terminating errors write an error to the error stream, but they do not 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 (http://go.microsoft.com/fwlink/?LinkID=145153).



<

RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/p/?linkid=294028
Write-Debug
Write-Host
Write-Output
Write-Progress
Write-Verbose
Write-Warning

REMARKS

<

Examples


-------------------------- EXAMPLE 1 --------------------------

PS C:\>Get-ChildItem | ForEach-Object { if ($_.GetType().ToString() -eq "Microsoft.Win32.RegistryKey") {Write-Error "Invalid object" -ErrorID B1 -Targetobject $_ } el
se {$_ } }



This command declares a non-terminating error when the Get-ChildItem cmdlet returns a Microsoft.Win32.RegistryKey object, such as the objects in the HKLM: or HKCU: dr
ives of the Windows PowerShell Registry provider.








-------------------------- EXAMPLE 2 --------------------------

PS C:\>Write-Error "Access denied."



This command declares a non-terminating error and writes an "Access denied" error. The command uses the Message parameter to specify the message, but omits the option
al Message parameter name.








-------------------------- EXAMPLE 3 --------------------------

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



This command declares a non-terminating error and specifies an error category.








-------------------------- EXAMPLE 4 --------------------------

PS C:\>$e = [System.Exception]@{$e = [System.Exception]@{Source="Get-ParameterNames.ps1";HelpLink="http://go.microsoft.com/fwlink/?LinkID=113425"}HelpLink="http://go.
microsoft.com/fwlink/?LinkID=113425"}
PS C:\> Write-Error $e -Message "Files not found. The $Files location does not contain any XML files."



This command uses an Exception object to declare a non-terminating error.

The first command uses a hash table to create the System.Exception object. It saves the exception object in the $e variable. You can use a hash table to create any ob
ject of a type that has a null constructor.

The second command uses the Write-Error cmdlet to declare a non-terminating error. The value of the Exception parameter is the Exception object in the $e variable.