Manage the Error Output of Commands
1. Problem
You want to display detailed information about errors that come from commands.
2. 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 Format-List cmdlet with the -Force parameter:
       $currentError = $error[0]
       $currentError | Format-List -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 category-based 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()
3. 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 Copy-Item cmdlet. If it fails to copy a file from one location to another, it can still proceed with the rest of the files specified.
A terminating error, on the other hand, indicates a deeper, more fundamental error in the operation. An example of this can again come from the Copy-Item cmdlet when you specify invalid command-line parameters.
Tags: cmd, cmdlet, command, error, export, manage, output, parameters
