Problem
You want to display detailed information about errors that come from commands.
Solution
To list all errors (up to $MaximumErrorCount) that have occurred in this session, access the $error array:
$error
To list the last error that occurred in this session, access the first element in the $error array:
$error[0]
To list detailed information about an error, pipe the error into the FormatList cmdlet with the Force parameter:
$currentError = $error[0]
$currentError | FormatList Force
To list detailed information about the command that caused an error, access its InvocationInfo property:
$currentError = $error[0]
$currentError.InvocationInfo To display errors in a more succinct categorybased view, change the $errorView variable to "CategoryView":
$errorView = "CategoryView" To clear the list of errors collected by PowerShell so far, call the Clear( ) method on the $error variable:
$error.Clear( )
Discussion
Errors are a simple fact of life in the administrative world. Not all errors mean disaster, though. Because of this, PowerShell separates errors into two categories: nonterminating and terminating.
Nonterminating errors are the most common type of error. They indicate that the cmdlet, script, function, or pipeline encountered an error that it was able to recover from or was able to continue past. An example of a nonterminating error comes from the CopyItem cmdlet. If it fails to copy a file from one location to another, it can still proceed with the rest of the files specified.
Aterminating error, on the other hand, indicates a deeper, more fundamental error in the operation. An example of this can again come from the CopyItem cmdlet when you specify invalid commandline parameters.
For more information on how to handle both nonterminating and terminating errors, see Chapter 13, Tracing and Error Management.