Share via


Exercise 1: PowerShell v2 Basic Features (Background)

Figure 1

A common request from the PowerShell community has been a tool to easily write and debug PowerShell scripts. PowerShell v2 comes bundled with an Integrated Script Environment (ISE) that will make PowerShell scripting much easier. The ISE includes a script pane, an output pane, and a command pane:

Figure 2

The command pane is similar to a PowerShell prompt, you can write and execute commands by entering them into the pane and hitting enter. Any output from the pane will be redirected to the output pane; which makes it easier to keep track of any results from previous commands. The Script Pane on top is the place where you can write and debug your scripts. The script pane fully supports tabs, which makes working with multiple scripts a more pleasant activity.

In order to assist you debugging your scripts , the ISE allows you setting breakpoints in a variety of ways. You can set them by manually setting a breakpoint from the ISE or you can also set them when a particular condition happens. The latter gives you finer control of when the breakpoint should be hit.

To set a breakpoint, you must specify the target script you are looking in debugging along with the line(s) where you would like to set the breakpoint:

PowerShell

Set-PSBreakpoint .\[Script-File-Name].ps1 -line X

In order to set more than one breakpoint, you just need to include the line numbers separated by a comma:

PowerShell

Set-PSBreakpoint .\[Script-File-Name].ps1 -line X,Y,Z

Figure 3

In order to set a breakpoint when a particular function is called, you can do by using Set-PSBreakpoint and using the –Command switch followed by the function name:

PowerShell

Set-PSBreakPoint -Command [Name-Of-Function]

PowerShell ISE’s debugger also allows you to set breakpoints when a particular variable is read or is written to. The following line shows how to set a breakpoint when a variable is read:

PowerShell

Set-PSBreakpoint -Variable [Variable-Name] -mode read

As you might have guessed it, the syntax to setting up a breakpoint when a variable is written to is:

PowerShell

Set-PSBreakpoint -Variable [Variable-Name] -mode write

Breakpoints have unique identifiers when they are set. You can use these identifiers to remove a particular breakpoint by using the Disable-PSBreakpoint command:

PowerShell

Disable-PSBreakpoint X