Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Get-Error

Gets and displays the most recent error messages from the current session.

Syntax

Newest (Default)

Get-Error
 [[-Newest] <Int32>]
 [<CommonParameters>]

Error

Get-Error
 [-InputObject <PSObject>]
 [<CommonParameters>]

Description

The Get-Error cmdlet gets a PSExtendedError object that represents the current error details from the last error that occurred in the session.

You can use Get-Error to display a specified number of errors that have occurred in the current session using the Newest parameter.

The Get-Error cmdlet also receives error objects from a collection, such as $Error, to display multiple errors from the current session.

Examples

Example 1: Get the most recent error details

In this example, Get-Error displays the details of the most recent error that occurred in the current session.

Get-ChildItem -Path /NoRealDirectory
Get-Error
Get-ChildItem: Cannot find path 'C:\NoRealDirectory' because it does not exist.

Exception :
 ErrorRecord :
 Exception :
 Message : Cannot find path 'C:\NoRealDirectory' because it does not exist.
 HResult : -2146233087
 TargetObject : C:\NoRealDirectory
 CategoryInfo : ObjectNotFound: (C:\NoRealDirectory:String) [], ParentContainsErrorRecordException
 FullyQualifiedErrorId : PathNotFound
 ItemName : C:\NoRealDirectory
 SessionStateCategory : Drive
 TargetSite :
 Name : GetChildItems
 DeclaringType : System.Management.Automation.SessionStateInternal
 MemberType : Method
 Module : System.Management.Automation.dll
 StackTrace :
 at System.Management.Automation.SessionStateInternal.GetChildItems(String path, Boolean recurse, UInt32 depth,
CmdletProviderContext context)
 at System.Management.Automation.ChildItemCmdletProviderIntrinsics.Get(String path, Boolean recurse, UInt32
depth, CmdletProviderContext context)
 at Microsoft.PowerShell.Commands.GetChildItemCommand.ProcessRecord()
 Message : Cannot find path 'C:\NoRealDirectory' because it does not exist.
 Source : System.Management.Automation
 HResult : -2146233087
TargetObject : C:\NoRealDirectory
CategoryInfo : ObjectNotFound: (C:\NoRealDirectory:String) [Get-ChildItem], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
InvocationInfo :
 MyCommand : Get-ChildItem
 ScriptLineNumber : 1
 OffsetInLine : 1
 HistoryId : 57
 Line : Get-ChildItem -Path C:\NoRealDirectory
 PositionMessage : At line:1 char:1
 + Get-ChildItem -Path C:\NoRealDirectory
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 InvocationName : Get-ChildItem
 CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Example 2: Get the specified number of error messages that occurred in the current session

This example shows how to use Get-Error with the Newest parameter. In this example, Newest returns the details of the 3 newest errors that occurred in this session.

Get-Error -Newest 3

Example 3: Send a collection of errors to receive detailed messages

The $Error automatic variable contains an array of error objects in the current session. The array of objects can be piped to Get-Error to receive detailed error messages.

In this example, $Error is piped to the Get-Error cmdlet. the result is list of detailed error messages, similar to the result of Example 1.

$Error | Get-Error

Parameters

-InputObject

This parameter is used for pipeline input.

Parameter properties

Type:PSObject
Default value:None
Supports wildcards:False
DontShow:False

Parameter sets

-Newest

Specifies the number of errors to display that have occurred in the current session.

Parameter properties

Type:Int32
Default value:None
Supports wildcards:False
DontShow:False
Aliases:Last

Parameter sets

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

Inputs

PSObject

You can pipe any PSObject to this cmdlet, but results vary unless either an ErrorRecord or Exception object is supplied.

Outputs

ErrorRecord

This cmdlet returns a PSExtendedError object.

Notes

PowerShell includes the following aliases for Get-Error:

  • All platforms:
    • gerr

Get-Error accepts pipeline input. For example, $Error | Get-Error.

Related Links


Feedback

Was this page helpful?