How to Create and Use an Add-On for the PowerShell ISE

Introduction

The PowerShell ISE has the capability to add Add-Ons to the PowerShell ISE environment. Add-Ins are user defined and can be implemented in PowerShell to integrate with the PowerShell ISE environment. Code snippets are built into the  PowerShell ISE (Accessed with 'CRTL. J in the editor) and is an example of an Add-On, and allows additional user-defined code-snippets to be added. Add-Ins are accessed from the Add-On menu option.

The key to writing an Add-On is understanding the PowerShell Object Model. The root of the Powershell object model can be accessed via the $psIDE automatic variable. The example Add-on discussed in this article can be downloaded from the PowerShell Add-On to Generate a Switch Template for an Enum gallery.  The Add-On works by being launched upon clicking an Add-On menu and displays dialogs for inputting or selecting an enum and generates a switch PowerShell source code and inserts the switch PowerShell code template into the current open file. For further background on enums, see the wiki article How to Create and Use Enums in Powershell.

The PowerShell Object Model

https://hyf8dw.blu.livefilestore.com/y2p7ibED2sQ9JmN5N18q8sQ7R3qjkUIpwiNXUirxiGAU7HULoyTU0hfNFj5DJU1bq9DhCVT9E0M0rLPSgqEzSdlPFySOkA0ocwy5H7MYDYCskk/EnumISEAddOn08.png?psid=1

The PowerShell object model starts with the root object $psIDE.  The $psIDE exposes the ISE scripting environment to an add-on, and by using the object properties can perform various tasks. $psIDE has a CurrentFile and CurrentTab properties and works the currently open file in the current tab. Each file object has an Editor property that can be used to peek at the text or select text or insert text.
 

Adding PowerShell ISE Add-On Menus

The PowerShell ISE has the capability to add Add-Ons menus to the PowerShell ISE environment. 

https://hyf8dw.by3301.livefilestore.com/y2phSWr0RysIo2a0o9ysbgJIjUc97K1FSywcyTK0W2uYQCy6ajmVkPzcNrEmllnun7iWBbpZqNc4NBHRCFdsN5RDJHGZUuxE03mWd0OcBVIVJs/EnumISEAddOn01.png?psid=1

The function to add the Add-on menus in the above screen picture. Nested menus are created by adding submenus to a menu.

Function Add-EnumISEAddOnMenus{

     If($psISE)

        {

        # The "_" is used for mapping to the fast key letter for the menu item.

         $EnumMenu = $psISE.CurrentPowerShellTab.AddOnsMenu.SubMenus.Add("_Enums",$null,$null)

        $EnumMenu.SubMenus.Add("Enum switch _code", `

               {Insert-EnumSwitchTemplate (Generate-InputEnumForm)}, "Alt+E") | Out-Null

        $EnumDialogMenu = $EnumMenu.SubMenus.Add("Enum Dialogs",$Null,$Null)

        $EnumDialogMenu.SubMenus.Add("_Input Enum Name Dialog",`

               {Insert-EnumSwitchTemplate (Generate-InputEnumForm)},"Alt+I") | Out-Null

        $EnumDialogMenu.SubMenus.Add("_Select Enum Name Dialog",`

               {Insert-EnumSwitchTemplate (Generate-SelectEnumForm)},"Alt+S") | Out-Null

        }

Else

        {

        Write-Output "Not in Powershell ISE"

        }

}  

 

PowerShell $Profile Automatic Variable

The PowerShell $Profile variable is read by the Powershell when it first opens and processes it to customize the Powershell environment. There is a $profile for both of the two Powershell environments; the ISE and the console. $Profile refers to the appropriate profiles for the environment.

In the PowerShell ISE

PS C:\> $profile

C:\Users\User\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

In the PowerShell console

PS C:\> $profile

C:\Users\User\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

For the add-on menus to appear each time PowerShell ISE starts up, the function to create the add-on menus is added to the PowerShell ISE profile.

PS C:\> Add-Content $Profile "Add-EnumISEAddOnMenus"

Sample PowerShell Add-On

The source code for the sample PowerShell ISE add-on can be downloaded from the PowerShell Add-On to Generate a Switch Template for an Enum gallery.

This PowerShell Add-On generates PowerShell source code for a switch construct based on a Enum. 

When the Add-On menu option is selected, a form to input a Enum name is shown.

http://gallery.technet.microsoft.com/site/view/file/126033/1/EnumISEAddOn03.png

Once a enum has been selected, Powershell source code is inserted into the current open file.

 http://gallery.technet.microsoft.com/site/view/file/126035/1/EnumISEAddOn07.png 

This function inserts the code into current file.

#Inserts the generated switch code template into the current file
Function Insert-EnumSwitchTemplate
{
  [CmdletBinding()] 
  param ( 
    [parameter(Mandatory=$True)] 
    [Object]$Enum 
  ) 
    Write-Verbose "Insert-EnumSwitchTemplate"
    If($Enum)
    {
        $Code = Get-EnumSwitchTemplate $Enum
        $File = $psISE.CurrentFile
        If ($File)
        {
            $File.Editor.InsertText($Code)
        }
    }
}