Using PowerShell $Error variable

The $Error variable hold a collection of information, and that’s why using $Error[0] can get to your error message objects.  Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.

Sample Console session:
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Users\maxt> $error 
PS C:\Users\maxt>
PS C:\Users\maxt> Ip[config
Ip[config : The term ‘Ip[config’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:10
+ Ip[config <<<<
    + CategoryInfo          : ObjectNotFound: (Ip[config:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\maxt>
PS C:\Users\maxt> $error[0]
Ip[config : The term ‘Ip[config’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:10
+ Ip[config <<<<
    + CategoryInfo          : ObjectNotFound: (Ip[config:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\maxt>
PS C:\Users\maxt> $Error[0].InvocationInfo.line
Ip[config
PS C:\Users\maxt> 

Starting a new PowerShell session the $Error will be empty.  Type a bad command “Ip[onfig” and the $Error variable will get populated.  Then we use the $Error[0] to display and access the rest of the information it holds.

Don’t forget to use the Get-Member to expose your PS variable objects.

PS C:\Users\maxt> $Error | Get-Member

   TypeName: System.Management.Automation.ErrorRecord

Name                  MemberType     Definition
—-                  ———-     ———-
Equals                Method         bool Equals(System.Object obj)
GetHashCode           Method         int GetHashCode()
GetObjectData         Method         System.Void GetObjectData(System.Runtime.Serialization.Serializatio
GetType               Method         type GetType()
ToString              Method         string ToString()
CategoryInfo          Property       System.Management.Automation.ErrorCategoryInfo CategoryInfo {get;}
ErrorDetails          Property       System.Management.Automation.ErrorDetails ErrorDetails {get;set;}
Exception             Property       System.Exception Exception {get;}
FullyQualifiedErrorId Property       System.String FullyQualifiedErrorId {get;}
InvocationInfo        Property       System.Management.Automation.InvocationInfo InvocationInfo {get;}
PipelineIterationInfo Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Int32,
TargetObject          Property       System.Object TargetObject {get;}
PSMessageDetails      ScriptProperty System.Object PSMessageDetails {get=& { Set-StrictMode -Version 1;

PS C:\Users\maxt> $Error.InvocationInfo
PS C:\Users\maxt> $Error[0].InvocationInfo

MyCommand        :
BoundParameters  : {}
UnboundArguments : {}
ScriptLineNumber : 1
OffsetInLine     : 9
HistoryId        : -1
ScriptName       :
Line             : ip[onfig
PositionMessage  :
                   At line:1 char:9
                   + ip[onfig <<<<
InvocationName   : ip[onfig
PipelineLength   : 0
PipelinePosition : 0
ExpectingInput   : False
CommandOrigin    : Internal

Finally, we can get deeper in the $Error[0] object to extract the line that failed during execution.  If you need to display the failed command line you can use the following line:

$Error[0].InvocationInfo.line

PS C:\Users\maxt> $Error[0].InvocationInfo.line
ip[onfig
PS C:\Users\maxt>

It’s all about understanding your .NET objects.  Hope this help!
🙂

%d bloggers like this: