Powershell script debugger
- #POWERSHELL SCRIPT DEBUGGER HOW TO#
- #POWERSHELL SCRIPT DEBUGGER INSTALL#
- #POWERSHELL SCRIPT DEBUGGER MAC#
Note that the name “PowerShell” in the launch.json file corresponds to the name in the drop down list in the Debug viewlet’s toolbar. That will open (or create) the launch.json for the open folder. You can access it directly by pressing the “Gear” icon in the Debug viewlet’s toolbar e.g.: Don’t bother trying to navigate to it from VSCode. The extension comes “pre-configured” with this launch.json file. In the PowerShell extension’s Examples directory you will find a folder name “.vscode” with this file in it.
#POWERSHELL SCRIPT DEBUGGER HOW TO#
The way VSCode knows how to start a debug session, including which file to start debugging, is a JSON settings file called launch.json.
VSCode’s Debug Configuration File – launch.json You will probably be surprised to find that the debugger started up the DebugTest.ps1 script and hit the breakpoint on line 17 in that script. Press the green “Start” button or press the F5 key to start debugging. Your VSCode editor should look like this: Type Ctrl+P and then “ Stop”, select StopTest.ps1 from the list. Let’s try debugging another script in this folder. Now press the “Stop” button in the debug toolbar or press Shift+F5. At the bottom of the Debug Console is a prompt from which you can modify variable values and execute PowerShell commands. It is displaying script output “Doing work…” from the function Do-Work. This is a REPL that, for now, is only available when you are debugging. Finally, notice the “Debug Console” window on the right. In the Call Stack section of the Debug viewlet, you can see that the debugger is one level down in the Call Stack – inside the Do-Work function. Notice that the editor, in the image above, is showing that script execution is paused on line 17 where we placed our breakpoint. The step buttons use the familiar keyboard shortcuts F10, F11 and Shift+F11. You can use this toolbar to start/stop debugging and step through your code.
When the debugger is running, the debug toolbar at the top center of the VSCode window will be visible. You can click that to bring up the Debug viewlet as well. First, note the “no bug” icon in the left-most area of the Side Bar. I want to point out a number of features above. Note: you can pull up the Debug view at any time by pressing Ctrl+Shift+D (on Windows). Your editor should now show the Debug viewlet in the Side Bar as shown below (sans the red boxes). Or you can also click in the left gutter (to the left of the line numbers) to set a breakpoint. Put the cursor on line 17 and press F9 to toggle a breakpoint on. In the Side Bar on the left, click on the Explore tab and then open the file DebugTest.ps1 e.g.: Open VSCode and then use File menu, Open Folder… to open the following folder:Ĭ:\Users\\.vscode\extensions\ms-vscode.PowerShell-\examples Now let’s get started debugging PowerShell scripts! I’ll let Adam cover the details of how that works when it releases in a future drop of the PowerShell Editor Services extension.
#POWERSHELL SCRIPT DEBUGGER MAC#
PowerShell MVP Adam Driscoll has been working on enabling debug support on other OS’s by means of remotely connecting to the extension running on a Windows machine from Linux or Mac OS. Even though VSCode is cross-platform, running on Windows, Linux and Mac OS, the debugging support provided by the PowerShell Editor Services extension currently runs only on Windows. One disclaimer about debugging PowerShell script in VSCode before we start.
#POWERSHELL SCRIPT DEBUGGER INSTALL#
As a pre-requisite see the post Getting Started with Visual Studio Code for Use with PowerShell on how to install VSCode and the PowerShell extension for VSCode. In this post we will look at how you can debug your PowerShell scripts using VSCode and the PowerShell Editor Services extension for VSCode.