If you’re just getting started with PowerShell, chances are you’ll be doing your work in the Integrated Scripting Environment (ISE). Although there are many third-party products that improve upon the features of ISE, Microsoft’s ISE is free and is more or less the de facto Microsoft PowerShell development tool. Sure, you can edit your PowerShell scripts in just about any text editor, including the venerable Notepad, but ISE is a much more productive tool, providing you with the ability to use IntelliSense and color-coded syntax as well as editing, executing, and debugging PowerShell scripts. In this column, I’ll show you 10 tips to make your PowerShell development in ISE more productive.
1. Put ISE on the Windows 8 Start Screen—Although PowerShell 3.0 and PowerShell ISE are both delivered out-of-the-box on Windows 8, there’s no PowerShell ISE option on the Windows 8 Start Screen or desktop, and if you search through Apps you won’t find it. That doesn’t mean that PowerShell ISE isn’t there. It’s hidden on the Administrative menu, which isn't displayed by default. To add the Administrative menu and the PowerShell ISE option to the Windows 8 Start Screen, open the Windows 8 Settings charm, choose the Tiles option, then move the Show administrative tools slider to Yes.
2. Set the Execution Policy—Oddly, although ISE is clearly oriented toward developing scripts, it does nothing to change PowerShell’s default script execution policy, which doesn’t allow scripts to run. The default PowerShell execution policy is set to Restricted. To allow ISE to run PowerShell scripts, go to the Console pane and enter the following command:
3. Open Multiple Tabs—One of the things that makes ISE so much more powerful than Notepad is that it lets you open multiple tabs and work on multiple scripts at the same time. Unlike Notepad, it doesn't isolate you in a single window. You can approximate this functionality with multiple Notepad windows, but then you lose out on the color coding, IntelliSense, and code snippets. To open multiple tabs, use the File, New option or the File, Open option, and ISE will open a new tab in the Scripting pane.
4. Use Snippets—If you’re not a developer, you might not know what code snippets are all about. Code snippets are prebuilt code blocks that you can insert into the Scripting pane to give you a head start in writing the correct PowerShell code. For example, if you want to use an If-Else statement but you don't remember the exact syntax, you can simply position your curser where you want the If-Else statement to start and then press Ctrl+J or select Start Snippets from ISE’s Edit menu. Doing so will display a dialog box with all the available snippets. As you scroll through the dialog box, a tooltip displays the actual PowerShell code that will be inserted.
5. Use Code Regions—Another feature that ISE provides to help you navigate your code is regions. Regions are collapsible sections of code indicated by a minus sign and an outline marker on the left side of the Script pane. ISE automatically creates regions for block structures, such as If-Else, For-Next, For-Each, and While loops. You can also create your own regions by marking the start of the region using the #region tag, optionally followed by a name. You mark the end of the region by using the #endregion tag. A closely related feature is PowerShell’s automatic brace matching. If you select a brace or parenthesis, ISE will automatically highlight the matching brace or parenthesis.
6. Use F1 PowerShell Help—As you might expect, ISE provides a lot of help for people who are just getting started with PowerShell. The Command Add-In pane on the right side of the screen can help you see the valid parameters for the various PowerShell cmdlets. The built-in F1 Help goes further by displaying a graphical pop-up window displaying Help for a selected PowerShell cmdlet. You can take advantage of the pop-up F1 Help by simply moving your cursor over a cmdlet that you want to display Help for and pressing F1.
7. Run Code—Although it might not be as full featured as some of the third-party PowerShell development products, ISE is completely capable of running and debugging PowerShell code. To run just part of a script, highlight the text you want to run and click the Run Script icon or press F5. Doing so will run just the selected code. To run the entire script, click the Run Script icon or press F5 without making a specific code selection. You'll see the results of the PowerShell code displayed in the Console pane.
8. Set Breakpoints—For a serious script developer, one of the most important features in ISE is its integrated debugger. You can use breakpoints to stop the execution of a given PowerShell script on a specific line. Breakpoints can be set on lines or variables. To toggle a breakpoint on a line, right-click on the line where you want the code execution to stop, then select Toggle Breakpoint from the context menu. Alternatively, you can click on the line and select Toggle Breakpoint form the Debug menu. You can also use the PowerShell set-psbreakpoint and get-psbreakpoint cmdlets to set and view breakpoints. You can’t set breakpoints on comment lines.
9. Single Step with the Debugger—Just as important as setting breakpoints is the ability to track the execution of your code by single stepping through the code. Single stepping though your code can help uncover logic problems that the code might have. The easiest way to single step is to press the F10 key after the code has halted on a breakpoint. You can also select Step Over from the Debug menu. If you have looping structures or functions that you want to step through, you can use F11 or Step Into from the Debug menu. Shift+F11 or Step Out will quickly exit the loop or function.
10. Examine Variables—Although stepping through your code is a valuable tool for uncovering logic errors, the ability to display the contents of variables is just as important. To display the contents of a variable, simply hover the mouse over any occurrence of the variable in the Script pane. You can also go to the Console pane and type in the variable name and press Enter. Of course, the execution of the script needs to be paused when you display a variable.