How it works...
Traditionally, a non-zero exit code means that the command or the script did not complete as intended. Querying the exit code told us whether there was an error in execution, and what the error was related to.
In PowerShell, we use the automatic variable $? to find out whether there was an error in the last command, or whether the last script exited in error; this addresses the first part.
The automatic variable $Error keeps track of all of the errors that occurred in the current PowerShell session. The last error gets stored as the first element in the $Error array. Errors in PowerShell have several properties, such as Exception (the name of the exception, as well as the description), TargetObject (the object on which the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access