Any script development usually requires troubleshooting, which usually results in one of two approaches. Manually set variables to values and run that data while the script is running, or use debugging to set breakpoints to inspect data before it continues or is aborted. Built into PowerShell ISE and Visual Studio Code is the ability to perform debugging.
How PowerShell Debugging Works
Included with Windows PowerShell, the PowerShell ISE is the traditional go-to environment for debugging PowerShell scripts. With a built-in debugging menu and graphical representation of breakpoints, it̵
- On the focused line, use the F9 key to set a break point.
- Right-click next to a line and choose Toggle Breakpoint to set a breakpoint.
- Use the
Set-PSBreakPointspecifying a rule, variable, function, or matching text.
Once you’ve set a breakpoint, you can easily start debugging by running the script with F5 or Debug → Run / Continue. Once the script reaches the specified breakpoint, execution will pause and you must take one of the following actions.
- Change – Only execute the current instruction and stop for the next instruction. If the current statement is a function or script, make the entire call before quitting.
- Get in – As with Step Over, execute only the current instruction and stop for the next instruction. Instead of running the entire function or script, you step into the call.
- To get off – If you are inside a function, level up and then level up if the function is nested. If it runs in the main script to the end of the script or the next breakpoint, whichever comes first. Skipped statements will be executed but not stepped through.
When the script is stopped at a breakpoint, you can hover the mouse cursor over a variable and see what the values of a particular variable are. In addition, you can also use the command line to run variables to see the current status. Here are a few caveats.
$Args will not work as they are automatic variables and not debugged. One way to get those values is to assign them to an intermediate variable that can then be set as a breakpoint.
PowerShell debugging in Visual Studio Code
Very similar to how the PowerShell ISE works, Visual Studio Code adds several very useful debugging options. In addition, VS Code allows easy debugging with PowerShell 7.x, while the ISE has traditionally been limited to only Windows PowerShell. Using the same key commands as the PowerShell ISE, you can quickly get started with debugging. Even better, you now have the debugging window within VS Code that consolidates much of the variable output and makes it easier to see a snapshot of all the variable values in your code.
Using VS Code Debugging
Like the PowerShell ISE, you can use F5 to start an interactive debugging session. Unlike the ISE, VS Code offers a much better and more useful debugging experience. Not only do you get the traditional Debug console, indicated by the
[DBG] prompt, but you have all known variables and their output in the left pane for debugging. To make navigating debug options better, the top debug bar makes it very easy to get in, out, and over breakpoints. You don’t have to save before you run either!
VS Code’s PowerShell extension is required for this to work easily.
Using the Debugging Commands command
While debugging strictly from the command line is sometimes not the easiest action, you can do it with the built-in debugging commands. There are a few built-in commands to make this easy.
Using the versatile
Set-PSBreakPoint, you can quickly set a breakpoint that will take you to the command prompt for debugging after hitting. At that point, you can quickly inspect variables using the standard keystrokes and continue or abort execution at that point.
Debugging doesn’t have to be difficult and once you get started using any of the above methods you will find that it is an integral part of your script development needs. Check out how to get started with PowerShell Debugging today!