Quickly Create a PowerShell Snippet in the ISE Using an Add On

As you know, starting with PowerShell V3, we have had the ability to create our own snippets within the PowerShell ISE. I actually covered doing this a while back using the New-ISESnippet cmdlet that is available to use so I won’t re-hash all of that for you. Instead, I will cover using the PSISE object to build an add-on that you can then use to quickly put together a snippet with little effort at all!

Looking at the $psISE object (note that this is only available when you are actually opened up in the ISE), you will need to look at the available methods under the CurrentPowerShellTab property and there you will see the AddOnsMenu property which contains all of the AddOns which are available to use for that particular tab.

$psISE.CurrentPowerShellTab.AddOnsMenu

image

Here we can see that there are no menus available at this point in time, but soon that will change.

Under this property is the SubMenus property, and more importantly its Add method which takes a number of parameters based on the parameter set that you use which contribute to the building of the AddOn menu.

image

For this article, I am going with the first set which means that we need to have a DisplayName, a ScriptBlock containing the code that will run and lastly a keyboard shortcut that we can use to quickly call the code in the menu.

Now that we have covered the basics here of building a menu addon, we can now build our menu to create a snippet. I’ll actually have 2 snippet builder menus, one to take the entire script that is in the window and create a snippet and another that will take a selected portion of a script and create a snippet from it. To make it a little more user friendly, I will use Microsoft.VisualBasic.Interaction to create some input boxes so the required information (Name and Description) can be added to the snippet.

Using the code below, we can create our addons that will then show up in the ISE as well as having their own keyboard shortcut to use.

Create a Snippet from Selected Code in Script File

[void]$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("Save-SelectedSnippet",{
    $Text = $psISE.CurrentFile.Editor.SelectedText
    If (($Text -notmatch '^\s+$' -AND $Text.length -gt 0)) {
        Try {
            [void][Microsoft.VisualBasic.Interaction]
        } Catch {
            Add-Type –assemblyName Microsoft.VisualBasic
        }
        $Name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Snippet Name", "Snippet Name")
        $Description = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Snippet Description", "Snippet Description")
        If ($Name -and $Description) {
            New-IseSnippet -Description $Description -Title $Name -Text $Text –Force
            Write-Host "New Snippet created!" -ForegroundColor Yellow -BackgroundColor Black
        }
    }
},"Alt+F8")

Create a Snippet from Whole Script File

[void]$psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("Save-Snippet",{
    $Text = $psISE.CurrentFile.Editor.Text
    If (($Text -notmatch '^\s+$' -AND $Text.length -gt 0)) {
        Try {
            [void][Microsoft.VisualBasic.Interaction]
        } Catch {
            Add-Type –assemblyName Microsoft.VisualBasic
        }
        $Name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Snippet Name", "Snippet Name")
        $Description = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Snippet Description", "Snippet Description")
        If ($Name -and $Description -AND ($Text -notmatch '^\s+$' -AND $Text.length -gt 0)) {
            New-IseSnippet -Description $Description -Title $Name -Text $Text –Force
            Write-Host "New Snippet created!" -ForegroundColor Yellow -BackgroundColor Black
        }
    }
},"Alt+F5")

 

As you can tell from the last parameters, the keyboard shortcuts are Alt-F8 for the selected code snippet and Alt-F5 for the snippet from the entire script file. This might seem familiar as this follows the same logic when it comes to running the code in the ISE (F5 runs all code and F8 runs selected code).

We can go back and verify that this worked a couple of ways first by looking at the properties of the menu…

image

… or by looking at the ISE menu itself…

image

And now we can see both of these in action.

Full Snippet

TestFullSnippet

Selected Snippet

TestSelectedSnippet

Enjoy!

About Boe Prox

Microsoft Cloud and Datacenter MVP working as a SQL DBA.
This entry was posted in powershell and tagged , , , . Bookmark the permalink.

6 Responses to Quickly Create a PowerShell Snippet in the ISE Using an Add On

  1. Pingback: Quickly Create a PowerShell Snippet in the ISE Using an Add On | Blog de Uriel Hdez ALM

  2. EM says:

    This is great. However, when I restarted the ISE, they had vanished. Snippets where there, but the “Add-On” menu items had gone.

  3. Bill says:

    Hi Boe. This is good info, but I’m trying to use your code. For some reason the ampersands appear to be causing me a problem and the I cannot get it to work. I cannot find the problem. Any thoughts.

    • Boe Prox says:

      Thanks for the heads up! The formatting that seems to happen when I publish seems to mess it up from time to time. It’s been fixed now and should work properly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s