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