How to Debug a PowerShell Script

How to Debug a PowerShell Script

Powershell logo

Any script boost continuously requires troubleshooting which continuously ends in for sure one of two approaches. Either manually environment variables to values and outputting that data when working the script, or the utilization of debugging to field breakpoints to peek data sooner than persevering with or aborting. Built-in to PowerShell ISE and to Visible Studio Code is the flexibility to create debugging.

How does PowerShell Debugging Work?

Integrated with Home windows PowerShell, the PowerShell ISE has been the worn journey-to atmosphere to debug PowerShell scripts. With a built-in menu for debugging and a graphical representation of breakpoints, it is easy to begin. First, any script that it is possible you’ll perchance perchance even be debugging must be saved. Next, it is possible you’ll perchance perchance have to field breakpoints the utilization of for sure one of three ways.

  • On the focused line sigh the F9 key to field a breakpoint.
  • Comely-click subsequent to a line and bewitch Toggle Breakpoint to field a breakpoint.
  • Use the Set of abode-PSBreakPoint specifying a line, variable, characteristic, or matched textual advise.

Whenever it is possible you’ll perchance perchance bear a breakpoint field, it is possible you’ll perchance perchance merely open debugging by working the script with F5, or Debug → Trip/Proceed. As soon as the script reaches the specified breakpoint, execution will seemingly be paused and also it is possible you’ll perchance perchance have to rob for sure one of the essential following actions.

  • Step Over – Attain simplest the fresh statement, stopping sooner than the following statement. If the fresh statement is a characteristic or script, attain the total call sooner than stopping.
  • Step Into – Identical to Step Over, attain simplest the fresh statement, stopping sooner than the following statement. As an replacement of executing the total characteristic or script, if called, step into the call.
  • Step Out – If internal a characteristic, step out and up one level if the characteristic is nested. If in the vital script attain till the stay of the script or subsequent breakpoint, whichever is first. Skipped statements are done nonetheless no longer stepped by.

When the script is stopped at a breakpoint, it is possible you’ll perchance perchance hover over a variable with the mouse cursor and watch what the values of any given variable are. Furthermore, it is possible you’ll perchance perchance sigh the declare-line to output variables as successfully to watch the fresh jam. There are about a caveats to this. $_, $Input, $MyInvocation, $PSBoundParameters, and $Args is no longer going to work as they’re automatic variables and no longer self-discipline to debugging. One plan to retrieve those values is to connect them to an intermediate variable that would possibly perchance perchance perchance then be field for a breakpoint.

PowerShell Debugging in Visible Studio Code

Very equivalent to how the PowerShell ISE works, Visible Studio Code provides lots of very precious debugging recommendations. Furthermore, VS Code lets you without arrangement back debug with PowerShell 7.x while the ISE is historically dinky to factual Home windows PowerShell. The sigh of the a similar key-instructions because the PowerShell ISE, it is possible you’ll perchance perchance snappy open with debugging. Even better is that you now bear the debugging pane internal VS Code which consolidates worthy of the variable output and makes seeing a snapshot of all variable values in your code more uncomplicated to take dangle of.

The sigh of VS Code Debugging

Correct kind just like the PowerShell ISE, it is possible you’ll perchance perchance sigh F5 to begin an interactive debugging session. Unlike the ISE, VS Code offers a seriously better and more precious debugging skills. Now no longer simplest lift out you procure the worn Debug console, indicated by the [DBG] steered, nonetheless it is possible you’ll perchance perchance bear the general known variables and their output in the left-hand debugging pane. To originate navigating debug recommendations better, the tip debug bar makes coming into into, out, and over breakpoints very easy. Furthermore, it is possible you’ll perchance perchance even be no longer required to assign sooner than working!

The PowerShell extension of VS Code is required for this to work without arrangement back.

The sigh of the Picture Debugging Commands

Even supposing debugging strictly from the declare-line is no longer the top possible operation to lift out at cases, it is possible you’ll perchance perchance lift out this the utilization of the built-in debugging instructions. There are about a built-in instructions to originate this straight forward.

The sigh of the versatile Set of abode-PSBreakPoint, it is possible you’ll perchance perchance snappy field a breakpoint that after hit will descend you into the debugging declare steered. At that point, the utilization of the usual key-combos it is possible you’ll perchance perchance snappy peek variables and continue or ruin execution at that point.

Conclusion

Debugging doesn’t have to be refined and at the same time as you open with this the utilization of any of the above methods, it is possible you’ll perchance perchance obtain it integral to your script boost needs. Take a look at out the style it is possible you’ll perchance perchance open with PowerShell Debugging this day!

Be taught More

Leave a Reply

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