24/7/365 Support

View the Errors Generated by a Command

Problem

You want to view the errors generated in the current session.

Solution

To access the list of errors generated so far, use the $error variable, as shown by Example 131.

Example 131. Viewing errors contained in the $error variable

PS >1/0 Attempted to divide by zero. At line:1 char:3

+ 1/0 <<<< PS >$error[0] | FormatList Force

ErrorRecord
: Attempted to divide by zero.

StackTrace
:
at System.Management.Automation.Parser.ExpressionNode.A

(...)

Message
: Attempted to divide by zero.

Data
: {}

InnerException : System.DivideByZeroException: Attempted to divide by zero. at System.Management.Automation.ParserOps.polyDiv(Execu val, Object rval) TargetSite : System.Collections.ObjectModel.Collection`1[System.Managem

ctions.IEnumerable) HelpLink : Source : System.Management.Automation

Discussion

The PowerShell $error variable always holds the list of errors generated so far in the current shell session. This list includes both terminating and nonterminating errors.

By default, PowerShell displays error records in a customized view. If you want to view an error in a table or list (through the FormatTable or FormatList cmdlets), you must also specify the –Force option to override this customized view.

If you want to display errors in a more compact manner, PowerShell supports an additional view called CategoryView that you set through the $errorView preference variable:

PS >GetChildItem IDoNotExist GetChildItem : Cannot find path 'C:\IDoNotExist' because it does not exist. At line:1 char:4

+ GetChildItem <<<< IDoNotExist PS >$errorView = "CategoryView" PS >GetChildItem IDoNotExist ObjectNotFound: (C:\IDoNotExist:String) [GetChildItem], ItemNotFoundExcep tion

To clear the list of errors, call the Clear() method on the $error list:

PS >$error.Count 2 PS >$error.Clear() PS >$error.Count 0

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say