Chapter 18. Debugging

Writing error-free code is the aspiration of pretty much every programmer. If you've spent any significant amount of time writing programs of any kind, you'll know that writing error-free code becomes increasingly difficult as the size of your code increases.

When you use Windows PowerShell on the command line, identifying many errors is simply a matter of spotting some slight syntax error. But Windows PowerShell is a scripting tool as well as a command line shell, so as with any significant programming language, you will need to carry out at least some debugging of your code when writing Windows PowerShell scripts. The longer and more complex your PowerShell scripts become, the more demanding it is to identify and fix all the errors that are present in them.

Debugging is the process of trying to identify and correct bugs in PowerShell scripts or commands. Often during initial development of a script you will observe undesired behavior of some kind. Spotting what is wrong can be easy or it can be hugely time-consuming and sometimes frustrating.

You might fail to spot some types of errors because you don't test edge conditions. Until users run your scripts in conditions you hadn't anticipated, the code seems to run correctly. When users bring an unanticipated combination of conditions to code execution, previously unknown errors may surface. I don't propose to explore those issues in depth in this chapter but will focus primarily on issues that are specific ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.