Tuesday, June 09, 2009

Included with the RC release of Windows 7 is PowerShell version 2 and a new editor called 'PowerShell ISE' (Integrated Scripting Environment).  Previously when I've been writing PowerShell scripts I've used PowerGUI as my script editor of choice so once I'd upgrade to Windows 7 I was keen to put the new PowerShell ISE to the test.

When I first started PowerShell ISE I get the standard screen as shown in the screen shot. I see there is a debug menu and with a script file open I can see there is reasonable support for syntax highlighting. PowerShell ISE

It's a relatively simple and clean interface, with a tabbed editor that allows you to easily move between scripts that you're editing and the toolbar isn't overly cluttered. However I did notice one thing slightly odd a slider control in the bottom right of the editor's window. This allows you to easily increase on decrease the size of the font used in the editor, not something I can see myself using that often as I'm normally set configuration like this up once in my development environment such as Visual Studio or Netbeans.

PowerShell ISE large text

One other point I noticed very early was the use of the Ctrl+W shortcut, in most web browsers this closes a tab and in PowerGUI it also closes the current editor window, however in PowerShell ISE the Ctrl+T and Ctrl+W key relate to output panel. My personal preference would have been for these to work with the script editor window.

The next thing I was keen to try was debugging, being well used to working with Visual Studio I tried to double click in the line number where I wanted the breakpoint, this didn't work but fortunately the keyboard shortcut F9, a small point but I'm always happy when there is some consistency between editors especially when they come from the same company. When debugging the shortcut keys F10 and F11 are also consistent with Visual Studio's debugging.

When debugging you can mouse over a variable name to see the value that has been assigned. However PowerGUI gives slightly more information as it will also tell you the type of the variable.

Debugging in PowerShell ISE Debuggin in PowerGui

 

 

 

One thing I did notice is that PowerShell ISE will break at the following line and allow you to step over it and wait at the next line, when I step over the second line PowerShell ISE will then display an error in the output window, however PowerGUI will report the error once I step over the first line and doesn't stop at the second line, to stop at the second line I needed to set a specific breakpoint.

lines of script

Another useful feature in PowerShell ISE is the ability to issue debug commands at the command window while debugging. For example just type the name of a variable and it's value will be displayed in the output window. PowerShell ISE DBG example

Other useful features are the ability to view the call stack using the keyboard shortcut Ctrl+Shift+D, and highlighting sshot-7a cmdlet and pressing F1 to get the help form that particular cmdlet. 

However despite the very good support for debugging PowerShell Scripts I prefer the intellisense offered by PowerGUI. PowerShell ISE does offer tab completion in the editor for cmdlet names however I prefer the Ctrl+Space option available in PowerGUI as this matches what I'm used to given that Visual Studio is mostly my day to day environment. I do appreciate that Tab completion option would be more familiar to System Admins. PowerGUI intellisense

But PowerShell ISE still has one trick up it's sleeve with it's programmability. PowerShell ISE has it's own object model thus allowing you to extend it with your own PowerShell script. You can add the script to the ISE profile and this will then be persisted between sessions. Note that this is a different profile than PowerShell and you can create the necessary file by typing the following command into the ISE command prompt:

if (!(test-path $profile )) {new-item -type file -path $profile -force}

Once the profile script has been created you can open it and start changing settings some settings. In Visual Studio I like to use the font 'Consolas' so these two lines of code will set ISE to use the font I like and at my preferred size when the editor starts.

$psise.options.fontname="Consolas"
$psise.options.fontsize=15

This is a very simple example of what's possible using the profile script to customize ISE but more complex customization is possible such as adding new menu items. If you put the following script into your profile and restart ISE you'll see a new entry on the menu bar called 'Add-ons', under this you will find an option 'Say Hi' click this and the famous 'Hello World!' message will be displayed in you editor pane.

function Get-Message
{
    $psise.CurrentPowerShellTab.Files[0].Editor.InsertText("Hello World!")
}
$null = $psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Say Hi", {Get-Message}, $null)

 

If you want to find out what members are available on the $psise object just issue '$psise | Get-Member' at the command window in ISE.

So which is best? Out of the box PowerGUI suits me the best due to the similarity with Visual Studio however I found the debugger better in PowerShell ISE. Also I'll be giving serious consideration to PowerShell ISE's programmability and experimenting with this to see how far I can push it, I'll post any interesting extensions I create.