Tips on how to Debug a PowerShell Script – CloudSavvy IT

Posted on

Powershell logo

Any script improvement normally requires troubleshooting which normally leads to considered one of two approaches. Both manually setting variables to values and outputting that knowledge when operating the script, or using debugging to set breakpoints to examine knowledge earlier than persevering with or aborting. Constructed-in to PowerShell ISE and to Visible Studio Code is the flexibility to carry out debugging.

How does PowerShell Debugging Work?

Included with Home windows PowerShell, the PowerShell ISE has been the standard go-to atmosphere to debug PowerShell scripts. With a built-in menu for debugging and a graphical illustration of breakpoints, it’s straightforward to get began. First, any script that you’re debugging should be saved. Subsequent, you have to to set breakpoints utilizing considered one of 3 ways.

  • On the centered line use the F9 key to set a breakpoint.
  • Proper-click subsequent to a line and select Toggle Breakpoint to set a breakpoint.
  • Use the Set-PSBreakPoint specifying a line, variable, operate, or matched textual content.

Upon getting a breakpoint set, you’ll be able to merely begin debugging by operating the script with F5, or Debug → Run/Proceed. As soon as the script reaches the desired breakpoint, execution might be paused and you have to to take one of many following actions.

  • Step Over – Execute solely the present assertion, stopping earlier than the subsequent assertion. If the present assertion is a operate or script, execute your entire name earlier than stopping.
  • Step Into – Just like Step Over, execute solely the present assertion, stopping earlier than the subsequent assertion. As a substitute of executing your entire operate or script, if referred to as, step into the decision.
  • Step Out – If inside a operate, step out and up one stage if the operate is nested. If in the principle script execute till the tip of the script or subsequent breakpoint, whichever is first. Skipped statements are executed however not stepped by.

When the script is stopped at a breakpoint, you’ll be able to hover over a variable with the mouse cursor and see what the values of any given variable are. Moreover, you need to use the command-line to output variables as nicely to see the present standing. There are a couple of caveats to this. $_, $Enter, $MyInvocation, $PSBoundParameters, and $Args is not going to work as they’re computerized variables and never topic to debugging. One option to retrieve these values is to assign them to an intermediate variable that may then be set for a breakpoint.

PowerShell Debugging in Visible Studio Code

Similar to how the PowerShell ISE works, Visible Studio Code provides a number of very helpful debugging choices. Moreover, VS Code permits you to simply debug with PowerShell 7.x whereas the ISE is historically restricted to only Home windows PowerShell. Utilizing the identical key-commands because the PowerShell ISE, you’ll be able to shortly get began with debugging. Even higher is that you simply now have the debugging pane inside VS Code which consolidates a lot of the variable output and makes seeing a snapshot of all variable values in your code simpler to understand.

Utilizing VS Code Debugging

Identical to the PowerShell ISE, you need to use F5 to start out an interactive debugging session. Not like the ISE, VS Code affords a a lot better and extra helpful debugging expertise. Not solely do you get the standard Debug console, indicated by the [DBG] immediate, however you’ve all the recognized variables and their output within the left-hand debugging pane. To make navigating debug choices higher, the highest debug bar makes moving into, out, and over breakpoints very straightforward. Additionally, you aren’t required to avoid wasting earlier than operating!

The PowerShell extension of VS Code is required for this to work simply.

Utilizing the Command Debugging Instructions

Though debugging strictly from the command-line will not be the simplest operation to do at instances, you are able to do this utilizing the built-in debugging instructions. There are a couple of built-in instructions to make this straightforward.

Utilizing the versatile Set-PSBreakPoint, you’ll be able to shortly set a breakpoint that when hit will drop you into the debugging command immediate. At that time, utilizing the usual key-combinations you’ll be able to shortly examine variables and proceed or break execution at that time.


Debugging doesn’t should be troublesome and when you get began with this utilizing any of the above strategies, you can find it integral to your script improvement wants. Try how one can get began with PowerShell Debugging at this time!

Source link

Gravatar Image
I love to share everything with you

Leave a Reply

Your email address will not be published. Required fields are marked *