Debugging scripts
From PowerGUI Wiki
Script Execution
You can run your script within the PowerGUI Script Editor itself, or directly within PowerShell.
When you run your script within the Editor, output from the script is written to the output pane. Breakpoints are only supported when the script is run within the Editor itself. To run a script within the editor, press F5.
When you run your script within PowerShell, a new PowerShell console is opened and your script is run in that console. To run a script within PowerShell, you can press Shift+F5.
Debugger Support
The PowerGUI Script Editor provides the following support for debugging:
| Option | Explanation |
|---|---|
| Breakpoints | When debugging, the PowerGUI Script Editor pauses execution every time it hits a breakpoint that you have defined.
Breakpoints can be turned on or off on any command by pressing F9 or selecting Debug|Toggle Breakpoint. |
| Run to Cursor | This feature executes a script until it hits the line where the cursor is currently located.
To activate this feature, put the cursor on the line you want and press Ctrl+F10 or select Debug | Run To Cursor. If a breakpoint is encountered before reaching the line of script where the cursor is located, execution will still pause on the specified breakpoint. |
| Execute Selection | This feature executes the portion of the PowerShell script that is currently selected. This is not the same as debugging a script, so breakpoints will be ignored. |
| Step through scripts | The PowerGUI Script Editor supports stepping through PowerShell scripts one command at a time. You can step through individual lines where each line is one command or through individual commands within one line on lines that contain a series of pipelined commands or semi-colon delimited commands.
You can debug from the first command of the script, or you can run to a breakpoint or the current cursor location and then step through your script from there. To start debugging, press F11 or select Debug | Step Into. After stepping into any script blocks or functions you can step out of them at any time by pressing Shift+F11 or selecting Debug | Step Out. |
| Watch Window | While debugging PowerShell scripts, the watch window will show you the current state of all variables that are used in your PowerShell script. As you step through your script, this window will automatically update the values of any variables that change. |
Debug | Debugger options
By selecting Debug | Debugger Options, you can choose from the following options:
| Option | Explanation |
|---|---|
| Run all scripts in the same runspace | Select if you want to continue using the same runspace when debugging.
Using the same runspace is faster; however, your environment will retain any changes that your script makes when you run it, which may alter the logical execution of your script on a subsequent run. |
| Reset PowerShell runspace each time debugging is started | Select if you want the Script Editor to start a new runspace for each debugging run.
Using a new runspace takes a little longer but this gives you a better impression of what the script will do in an environment where it has not been previously run. |
| Load PowerShell profiles | Select if you want the profile loaded when debugging. |
See also Passing script parameters
