| Downloads |
|---|
| 104713.zip |
In the first version of Windows PowerShell, Microsoft didn't provide an integrated development environment (IDE) for PowerShell. In PowerShell 2.0, the PowerShell team filled that gap by adding a PowerShell IDE called the Integrated Scripting Environment (ISE). It provides an easy-to-use interface for editing and debugging scripts.
Figure 1 shows the ISE window, and Table 1 describes the elements labeled in Figure 1. The ISE window is divided into three panes by default:
Floating the mouse cursor over the toolbar buttons displays the buttons' functions in pop-up tooltips. The ISE's shortcut keys aren't shown in the toolbar button tooltips, but they are displayed in the menus.
All of PowerShell's Help topics are available in the ISE. Press F1 to access them. If you position the cursor on a cmdlet name, pressing F1 will open the PowerShell Help file to that cmdlet's Help page.
The tabs at the top of the script pane represent open script files. When you start a new script (select File, New or press Ctrl+N) or open an existing script (select File, Open or press Ctrl+O), the ISE adds a new script tab (element D) to the top of the script pane.
In addition to script tabs, you can also open a new PowerShell tab (element C), which appears above the script tabs, as shown in Figure 1. Each PowerShell tab represents a new PowerShell instance, or execution environment, inside the ISE. This means that variables, functions, and aliases that you create in one PowerShell tab aren't visible when you switch to a different PowerShell tab. (Opening a new PowerShell tab is like starting a new powershell.exe console instance.) You open a new PowerShell tab by selecting File, New PowerShell Tab or by pressing Ctrl+T. Note that when only one PowerShell tab is open, it's not shown.
You can adjust the ISE's three panes several different ways using the View menu:
There are four ways you can open a PowerShell script for editing in the ISE:
psEdit C:\Scripts\TestScript.ps1
loads the file TestScript.ps1 into a new script tab. An error will appear in the output pane if the file doesn't exist.
You can open multiple scripts in the ISE using any of these four techniques. Each script will open in its own script tab. Table 2 lists the keystroke commands available in the ISE's script editor.
Once a PowerShell script is loaded into the script pane, you can run it by choosing Run on the File menu or by pressing the F5 key. PowerShell will run the script as if you typed the script's filename in the command pane and pressed Enter. If you want to run only a portion of a script, select the code you want to run and choose Run Selection on the File menu or press F8. PowerShell will execute the code you've selected as if you entered it in the command pane. In both cases, the output appears in the output window.
The ISE lets you debug PowerShell scripts from its easy-to-use interface. Debugging is the process of suspending a PowerShell script as it's running so you can identify and correct problems in the code. To debug a script in the ISE, load it into the ISE's script pane, set one or more breakpoints that suspend the script, and examine the values of variables while the script is suspended to determine the cause of the problem.
For example, MathTest.ps1 in Listing 1 is a script with a single function called math-power that contains a logic error.
Listing 1: MathTest.ps1
# Raises a positive whole number to a power.
function math-power(\\[Int\\] $number, \\[Int\\] $power) \\{
if ($power -eq 0) \\{
1
\\}
else \\{
$result = $number
for ($i = 1; $i -le $power; $i++) \\{
$result *= $number
\\}
$result
\\}
\\}
math-power 2 2 # Should output 4.
I'll use this script to demonstrate how to use the ISE's debugging capabilities. To follow along, download MathTest.ps1 by clicking the Download the Code Here button near the top of the page and save the 104713.zip file on your machine. After you've extracted MathTest.ps1, follow these steps:
The debugger is much more useful with larger scripts that have many variables, but this example should help you get started with using this helpful tool.
The ISE is a truly valuable PowerShell enhancement. I've provided only a cursory overview of its script editor and debugger. I recommend that you add the ISE to your PowerShell toolkit by downloading PowerShell 2.0. This version is installed with Windows 7 and Windows Server 2008 R2, so you don't need to install PowerShell 2.0 separately if you're using those OSs. If you have Server 2008, Windows Server 2003, Windows Vista, or Windows XP SP3, you need to install the Windows Management Framework Core package from http://support.microsoft.com/kb/968930. Depending on your OS, you might also need to install .NET Framework 2.0 SP1 (for PowerShell 2.0) and .NET Framework 3.0 (for the ISE).