It can save you time and aggravation
| Downloads |
|---|
| 136036.zip |
The Windows PowerShell Integrated Scripting Environment (ISE) is a combined shell, debugger, and script editor that comes with PowerShell. Considering its capabilities, it's significantly underused. After I talk about the reasons why the PowerShell ISE is underused, I’ll explore how to take advantage of it.
Before getting started, however, let me mention the two things you can’t do with the PowerShell ISE:
Based on my experiences talking with people, a common reason why the PowerShell ISE is underused is a lack of knowledge about its capabilities. When I say the ISE is underused, I don't just mean people using the PowerShell console application instead of the ISE. I also mean people using the PowerShell ISE who haven't taken advantage of its labor-saving features.
A second reason for the underuse is that on Server 2008, only the console version of PowerShell is installed by default. The PowerShell ISE isn’t installed because it uses the Microsoft .NET Framework 3.5.1, which isn’t installed. If installing the .NET Framework 3.5.1 isn’t a problem, you can install the PowerShell ISE without rebooting. Here's how: From the server console, open Server Manager, go to Features, then choose Add Feature. Select Windows PowerShell Integrated Scripting Environment from the feature list, and click Install. Windows will prompt you for confirmation that you also want to install the .NET Framework 3.5.1. Click Yes, then proceed with the installation.
Alternatively, from a console PowerShell prompt, you can run the following two commands:
An immediate benefit of using the PowerShell ISE is its general features as a shell, not a script editor. You get access to many of the same keyboard accelerators and other conveniences that are standard across graphical applications.
So, Ctrl+A selects everything in a pane, Ctrl+C copies, Ctrl+X cuts, and Ctrl+V pastes. Although not earth shattering, this feature repeatedly saves you little bits of time and effort. The complete list of keyboard shortcuts for the PowerShell ISE is available at Microsoft TechNet.
The accessibility of this graphical application is even more significant. Although the benefits are easy to overlook when you're used to working with graphical applications, the PowerShell ISE provides:
Another graphical element I find useful is the ability to have several sessions and scripts open at the same time in one instance. You can run up to eight PowerShell sessions at a time in the same window and a large number of scripts. (How large I can't say; I've had up to 300 short scripts open for editing at one time.)
At first glance, it might seem that a GUI focus is in conflict with PowerShell's philosophy. However, there’s no conflict. PowerShell's tie to the console window isn’t APIs but rather the concept of pipeline-based work processing. Making use of any tool that simplifies work is the real PowerShell philosophy.
One of PowerShell 2.0's features is session remoting. The PowerShell ISE walks you through setting up an interactive PowerShell session on a remote machine. To begin, select New Remote PowerShell Tab from the File menu (or just press Ctrl+Shift+R). You'll get the prompt shown in Figure 1. After you enter the information and click Connect, you’ll get the standard credentials dialog box.
Alternatively, you can use the Enter-PSSession command to set up an interactive PowerShell session on a remote machine. However, I find that setting it up through the File menu saves a bit of time and a lot of annoyance, making the New Remote PowerShell Tab option one of my favorites.
I still prefer to use the two text editors I know well (SAPIEN Technologies' PrimalScript and Helios Software Solutions' TextPad), but if you're on the go or don't have a favorite editor, the PowerShell ISE beats Notepad hands down, even for editing files that aren't scripts.
For PowerShell scripts and modules, the ISE supports tab-completion of command and variable names and provides syntax coloring for various kinds of tokens (e.g., commands, parameter names, keywords). You can also open any text file in it, which I find useful at client sites since the ISE supports using regular expressions in search and replace operations, as Figure 2 shows. The Match case and Whole word options work with or without regular expressions.
You can debug scripts from a PowerShell console, but the PowerShell ISE definitely makes it easier. Here's how to debug a script in the PowerShell ISE: Open the script you want to debug in the PowerShell ISE. Note that you have to save a script before you can debug it. Debugging operations are disabled in a new, unsaved .ps1 script that’s being edited in the PowerShell ISE.
Next, go to the line where you would like the execution to pause and select Toggle Breakpoint from the Debug menu (or press the F9 key). When setting breakpoints, keep the following in mind:
After setting the breakpoints, choose Run/Continue on the Debug menu (or press the F5 key). The ISE will run the script to the first breakpoint you set, then halt. You can then view the current values of variables by hovering your mouse over them in the script editor. You can also display the call stack (i.e., the list of script blocks that the code has entered) by selecting Display Call Stack from the Debug menu. To continue the script’s execution, choose Run/Continue on the Debug menu (or press F5) again. You can manually stop the debugger at any time by selecting Stop from the Debug menu (or by pressing Shift+F5).
You can also use the debugger with scripts that have mandatory command-line parameters. After setting breakpoints in the script, simply enter the script’s name and its required parameters at the PowerShell prompt instead of using the Debug menu or pressing F5 to start debugging. The script will automatically run in the debugger. To see the debugger in action, check out the article “Editing and Debugging Scripts with PowerShell 2.0's Integrated Scripting Environment.”
The PowerShell ISE has another useful feature of particular interest to advanced users: You can access the ISE as a PowerShell object through the $psISE variable, then extend or customize the ISE.
Listing 1 provides several examples of how you might use the $psISE variable. The code in callout A uses it to extend the Add-ons menu. This menu isn't visible until you add a customization. In this case, a display name, script block, and keyboard shortcut are being added.
You can use the $psISE variable to save all the output from the PowerShell tab you’re on, as the code in callout B shows. You can also use the variable to set the ISE’s default font size and family, as the code in callout C shows. This is an example of a customization that some people might like putting in a PowerShell profile script.
You can download the code in Listing 1 by going to the top of this page and click the 136036.zip hotlink. If you’d like more information on how to customize and extend the PowerShell ISE, see the "Windows PowerShell Integrated Scripting Environment (ISE) Help" topic in the graphical Help file that comes with the ISE. It covers the entire object model for the PowerShell ISE at length.
Like most other small tools, using PowerShell ISE is generally a matter of preference. With that said, if you don't already use a modern text editor, I recommend that you try the PowerShell ISE. Its syntax highlighting and tab completion can reduce effort and errors significantly. Plus, its support for using regular expressions in search and replace operations is invaluable if you occasionally need to perform complex search and replace operations on large text files—PowerShell scripts or not.