Category Archives: ISE

Improving the PowerShell ISE Experience with ISESteroids 2

For a long time I’ve used the built-in to Windows, PowerShell ISE for my PowerShell scripting experience. Most people tend to have a particular favourite editor for their coding, usually after trialling out a few different ones. For pretty much everything else I’ve settled on Sublime Text, but for PowerShell I use the ISE since I really like the integration with the PowerShell console.

The ISE was introduced in PowerShell 2.0 and to be honest was pretty basic back then. It’s  improved significantly since then into version 4, but still has some areas where there could be improvement or features missing that you would like to see.

Earlier in the year I tried out ISESteroids 1.0 which started to plug a number of the gaps I found in the ISE. Recently I had chance to upgrade to ISESteroids 2 and it has improved even further.

For a quick preview of what is available check out the below video.

A few things in it I particularly like:

1) More distinct (yellow) highlighting of bracket matching or other sections

ISESteroids01

(single click on this double quoted string)

ISESteroids02

2) Block commenting (this was a real annoyance for me – there is a keyboard shortcut to do it in the standard ISE, but still fiddly)

Before:

ISESteroids03

After pressing the single button below:

ISESteroids04

ISESteroids05

 

3) ScriptMap which allows you to easily navigate your way through long scripts

ISESteroids06

 

4) Manage Version History

ISESteroids07

Clicking on Compare opens WinMerge and a view of what has changed between versions

ISESteroids08

5)  Autoselection. Click repeatedly to select various code segments

ISESteroids12

ISESteroids09

ISESteroids10

ISESteroids11

 

6) Enhanced Debugging

Best explained in the following video

For a more in-depth look at some of the features, check out the below video with ISESteroids creator Dr Tobias Weltner and fellow PowerShell MVP Jeff Wouters.

Creating PowerShell ISE v3 (and later) Code Snippets

When using the PowerShell ISE , similar to other scripting editors, you have access to what are known as ‘code snippets’. These are quick start ways to generate frequently used code, for instance if there is something you use regularly and can never remember the syntax for, or maybe it is too long to be practical to remember it.

PowerShell ships with some default snippets and it is also possible to add some custom snippets of your own. They can be accessed via the keyboard shortcut Ctrl-J – the below screenshot displays the default snippets available.

ISESnippet01

Selecting one of these snippets will populate the editor with the sample code. In the example below I picked the switch snippet, not one that’s always easy to remember, and an example is placed in the editor which I can modify for my needs.

ISESnippet02

Naturally, you may want to create some of your own, known as User-Defined Snippets. PowerShell v3 ships with some cmdlets to help you create and manage these, starting with the New-ISESnippet cmdlet. Give the code you wish to use for your snippet to this cmdlet and it will create a <Title>.Snippets.ps1xml file in the $home\Documents\WindowsPowerShell\Snippets directory with the title that you specify .

For example, one thing that students in the PowerShell classes I teach often struggle to remember is the syntax for creating a calculated property with Select-Object, like say this:

Get-ChildItem C:\Test | Select-Object Name, CreationTime,  @{N="Kbytes";E={$_.Length / 1Kb}}

So, we can create a snippet to help with this via the following (Note, the CaretOffset parameter places the cursor in a specific place after using the snippet, in this case at the start of the word ‘Title’ that you need to edit. Aren’t I helpful?!):


New-IseSnippet -Title "Calculated Property" -Description "Syntax for a Calculated Property" -Text "@{N='Title';E={ }}" -CaretOffset 5

This will create the below XML file in my  snippets folder for both current and future use every time I open the PowerShell ISE.

ISESnippet03

Now if I get to a point where I can’t remember the syntax for a calculated property….

ISESnippet04

I can select the snippet and have it populate that difficult to remember syntax for me:

ISESnippet05

ISESnippet06

So that is cool for short syntax items, but what about something which is multiline, how would I create a snippet for that? One answer is to use a Here-String.

For example, say you wish to add a parameter block like this as a snippet


Param
 (

[parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [ValidateNotNullOrEmpty()]
 [String[]]$Parameter1,

 [parameter(Mandatory=$false,ValueFromPipeline=$false)]
 [ValidateNotNullOrEmpty()]
 [PSObject[]]$Parameter2
 )

First of all store this text in a Here-String then use that Here-String with New-ISESnippet . Tip: ensure you use single quotes rather than double to create the Here-String otherwise the variables will be expanded to blank, since they (probably) don’t exist.


$Snippet = @'
 Param
 (

[parameter(Mandatory=$true,ValueFromPipeline=$true)]
 [ValidateNotNullOrEmpty()]
 [String[]]$Parameter1,


 [parameter(Mandatory=$false,ValueFromPipeline=$false)]
 [ValidateNotNullOrEmpty()]
 [PSObject[]]$Parameter2
 )
'@

New-IseSnippet -Title "Param Block" -Description "Syntax for a Param Block" -Text $Snippet

Now I can create a Param Block with this multiline snippet

ISESnippet07

ISESnippet08

and our snippets folder contains two XML files, one for each snippet:

ISESnippet09

Having multiple snippet files may or my not be a good thing, depending on your scenario. It’s possible if you want to share these that you may want to combine them into a single file. This post on the PowerShell team blog details an XML snippet example and its structure. If we take the content between <Snippet Version=’1.0.0′> and </Snippet> we can combine each snippet into one file which is maybe easier for distribution to others if you want them to have all of those snippets.

ISESnippet10

Next week I’ll be posting some snippets that are part of my UKVMUG session which you may find useful, all combined into one file…….