5 Tips for PowerShell 3.0 ISE

Posted on March 13, 2013 by Jeff Hicks in PowerShell with 0 Comments

Recently I wrote an article in which I discussed 5 Tips and Tricks for the PowerShell 3.0 console. It only seemed fitting to write a complementary article on the PowerShell 3.0 ISE, or Integrated Scripting Editor. The PowerShell ISE has been redesigned for 3.0. Today I’ll tell you about five of my favorite tips for getting the most out of this PowerShell ISE.

1. Region Folding

I love that PowerShell’s ISE will collapse regions of code. The region can be a scriptblock like an IF statement or you can create your own regions. In your script start with a comment like this.

#region get computers

Hit enter a few times and then type this line:

#endregion

The ISE will automatically create a collapsible region. Write your code in between the comments. This is a great way of organizing your code and getting it out of the way when you don’t need it. Use Ctrl+M to toggle expanding and collapsing regions. In the ISE my script can look like this.

PowerShell 3.0 ISE region

Or I can expand individual sections by clicking on the plus (+) sign.

PowerShell 3.0 ISE region

Notice that constructs like ForEach and the If statement are also collapsed. You’ll automatically get the collapsible region with a scriptblock, but I like defining my own. You don’t have to type anything after #region, but if you don’t then you have no way of knowing what code is tucked away.

2. Snippets

The new version of the ISE also supports snippets and ships with a number of commonly used code snippets. In the ISE, press Ctrl+J to access the snippets.

PowerShell 3.0 ISE snippet

Start typing the snippet name until the ISE selects the one you want and then press Enter. The snippet text will then be entered into your script.

Snippets are stored under your Windows PowerShell folder in XML files. You can see them with the Get-IseSnippet cmdlet.

PowerShell 3.0 ISE snippet

Even better, you can create your own snippets. Define a block of text.

You can then use the New-ISESnippet cmdlet to create it.

This will create the XML file and load the snippet into the ISE memory so that it will show up when you press Ctrl+J. Because the snippet is stored in the XML file, it will be available the next time you start the ISE. To get rid of it, you’ll need to delete the XML file.

The snippet will remain in memory until you restart the ISE.

Sponsored

3. Popup Help

Here’s a handy tip I came across recently: In the script editor, position your cursor anywhere in a cmdlet name and press F1. You will get popup help for the cmdlet.

PowerShell 3.0 ISE popup

This is the same window you get when you use the new –ShowWindow parameter with Get-Help. The ISE simplifies the entire process to a single key — F1. This works for any command or function that has help, even functions from your own modules.

For best results, separate the command from operators. You might have written a line like this in the past — I know I have:

$s=get-service

If your cursor is anywhere in Get-Service and you press F1, nothing will happen because the ISE can’t tell where the command begins and end. So write the code like this:

$s = get-service

4. Themes

It is much easier in the new ISE to modify color settings. In fact, we now have themes. You can access themes under ToolsOptionsManage Themes.

PowerShell 3.0 ISE theme

The change is immediate.

PowerShell 3.0 ISE theme

Themes which are persistent, which means you don’t need to configure your profile to adjust color schemes. You can also create your own themes and import. In fact, you can download this theme file and import it into your ISE. This will set your ISE to use a VIM-like color and font scheme. If you need to change it back, pick a different theme or the default theme.

Sponsored

5. Parentheses Matching

A common bug for many scripters, myself included, is forgetting to add the closing parentheses or curly brace. This is especially problematic when you have some nested constructs like this where there are several closing braces:

If ($Validate) {
foreach ($computer in $computers) {
if (Test-Connection $computer -Count 2 -ea SilentlyContinue) {
$online+= $Computer
}
else {
$offline+= $computer
}
}
}

In the ISE, you can select a brace (or parentheses) and the matching one will be highlighted.

PowerShell 3.0 ISE parentheses

It might be hard to detect in the screenshot above, but the opening brace on line 23 is matched with the one on line 30. If the matching brace is out of view, press Ctrl+] to jump to it. You can jump forward or backward. You can also collapse the region at the opening brace and see if it ends where you expect it. Personally, I like adding a comment to the closing braces so I can tell which one goes where, especially if there are a number of lines between the open and close.

PowerShell 3.0 ISE parentheses

The PowerShell 3.0 ISE is a much improved and friendlier tool than what we had in v2. I hope you’ll use some of these tips to become more productive and efficient when developing your own scripts. If you have a handy ISE 3.0 tip or trick, I hope you’ll share it in the comments below.

 

Sponsored

Tagged with , ,