Terminating Errors
Non-Terminating Errors
Figure2 - Non-Terminating Error Output
ErrorAction to Stop. For example, Get-ChildItem "missing_dir" -ErrorAction 'Stop'Error Views
NormalView of the $ErrorView
preference variable. This view was standard and traditional until
PowerShell . Starting with PowerShell , the default view has changed
to what you see in Figure 2 and that is of the ConciseView.
It dispenses with much of the decoration around the output, but as you
might be able to tell, some information is not made available.The Error Object Behind the Scenes
$Error
object that is populated by PowerShell when errors are thrown. To view
this data, you are able to output and walk through the information. The
traditional way to get the last error thrown is by calling $Error[0]. This uses array notation to reference the error.As you can see there is the same error as originally shown, but we want to view more of the data. By selecting all of the properties, we are able to see what’s available. As we will talk about in the next section, the Get-Error cmdlet provides a rich view of this data, but it’s important to understand what is going on underneath.
The New Get-Error Cmdlet
Get-Error cmdlet. To expand upon the ConciseView and show far more detail, we can run the Get-Error cmdlet and see the expanded details of the last error thrown.Exception
- Type – Basic Exception Information
-
ErrorRecordMost of this information is from the
$Errorobject itself. TheTargetObject,CategoryInfo, andFullyQualifiedErrorIdare all duplicated further in theGet-Erroroutput. What is useful is theExceptiondata.- Type – An exception, but could be referencing the parent exception
- Message – The human-readable error message
- HResult – Traditional numerical error code that Windows has used since the early days of the operating system
-
ItemName – The same as the
TargetObjectshown later in theGet-Erroroutput -
SessionStateCategory – A series of values that errors fall into, this is an
enumunderneath - TargetSite – A set of information that exposes some of the internal PowerShell engine values and where the error itself is coming from
- StackTrace – This is the actual method signature of where the error itself came from and can help aid in why an error was shown
- Message – The human-readable error message
- Source – This is the source of where the error is coming from
- HResult – As discussed above, the traditional numerical error code from Windows
TargetObject
D:\\missing_dir CategoryInfo
<Error>: (<TargetObject>:<ObjectType>) [<Originating CmdLet>], <Exception Type> FullyQualifiedErrorId
FullyQualifiedErrorId is Message property of the exception object combined with the fully-qualified name of the class where the exception originated.InvocationInfo
- MyCommand – The originating cmdlet or function throwing the error
- ScriptLineNumber – Location within the file or ScriptBlock that the error is thrown
- OffsetInLine – The location within the line that the error was thrown
-
HistoryId – The location from within the
Get-Historycmdlet that the error was thrown - Line – The command throwing the error
- PositionMessage – Combined information for the error
- InvocationName – The cmdlet or function throwing the error
- CommandOrigin – In what context the error was thrown
ScriptStackTrace
line 1, but this will reflect the line of the error in the given ScriptBlock.Conclusion
Get-Error cmdlet. Furthermore, the ConciseView of the ErrorAction preference will keep the command line free from clutter and make coding even easier!















