Chapter 11. A template for handling and reporting errors
Writing programs that work when everything goes as expected is a good start. Making your programs behave properly when encountering unexpected conditions is where it really gets challenging.
Efficient, predictable error handling has been at the core of PowerShell’s design since its earliest days and has allowed scripters to design robust automation solutions with minimal effort. In small scripts a balanced combination of common parameters, the use of Write-Error, or perhaps Write-Warning, and some well-placed if/then statements generally prove to be enough for most scripter’s daily automation problems. ...