Adding Custom Menu Items to the Windows PowerShell ISE

Summary: Microsoft Scripting Guy Ed Wilson provides step-by-step instructions for adding custom menu items to the Windows PowerShell ISE.

 

Microsoft Scripting Guy Ed Wilson here. A long long long long time ago in a far far far far away land, I used to write code in a language called WordBasic. Using Word Basic I could completely automate Microsoft Word, and in so doing I wrote several mission-critical applications that were used by the company where I was employed. Because I was doing a lot of Word development, I had the most tricked-out version of Microsoft Word you ever saw. I do not think anything was left untouched, nor did any default setting escape unscathed. I had custom menus for everything, and custom bits of code that ran when I pressed specific keystroke combinations. It was cool!

Of course, I used to customize Windows 3.1 as well. I would record special sounds and attach them to various Windows events and the like. These days, my life is rather boring, and if I change the wallpaper on my 64-bit Windows 7 Ultimate desktop, I am doing something exotic. Perhaps I have grown jaded in my access to computing power, or become complacent with the Windows defaults. Maybe it is the difficulty in changing things back to the default Windows configuration so I can take screenshots for the books I write. I do not mind taking screenshots for books because pictures make the book easier to read. However, I loathe retaking screenshots. In fact, I hate rework of all kinds. One of the things that Microsoft Press is really a stickler on is a “default configuration,” so if I happen to have a cute fuzzy koala bear picture I took while I was in Australia as my background, such as the one shown in the following image, I will be in for some rework.

Photo Ed took of fuzzy koala bear

With the Windows PowerShell ISE, I am starting to get back into the “fun mode” of scripting and customization. The fact I can add custom menus and assign shortcut keys to menu items means that the Windows PowerShell ISE is highly customizable. And it offers a “tweak geek” myriad opportunities for self-expression.

The Add-MenuItems.ps1 script adds several menu items to the Windows PowerShell ISE. It adds a submenu item that restores all defaults for the Windows PowerShell ISE, as well as one that only restores the token color defaults. In addition, two other submenus are added, both of which run scripts. The first one runs the Get-PsISEFonts.ps1 script, and the second menu item runs the Get-PSIseColorValues.ps1script.

A third script is available as a menu item that runs the Set-IseColorsAndFonts.ps1 script.

One last menu item runs a command to remove the menu items from the Windows PowerShell ISE. Keep in mind that the three scripts that are run use a hard-coded path, so you will need to modify it to fit your particular environment. The complete Add-MenuItems.ps1 script is seen here.

Add-MenuItems.ps1

$RestoreRoot = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “Restore”
,$null,$null)
$RestoreRoot.Submenus.Add(“RestoreALLDefaults”, 
 
{$psISE.Options.RestoreDefaults()}, “Ctrl+Alt+R”)
$RestoreRoot.SubMenus.Add(“RestoreTokenColorDefaults”, 
 
{$psISE.Options.RestoreDefaultTokenColors()}, “Ctrl+Alt+T”)

$GetOptions = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “GetOptions”
,$null,$null)
$GetOptions.SubMenus.Add(“GetFonts”, { C:\fso\Get-PsISEfonts.ps1 } , $null)
$GetOptions.SubMenus.Add(“GetColors”, { C:\fso\Get-PsIseColorValues.ps1 } , $null)

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “MyCustomISE”
, { C:\fso\Set-PsISEcolorsAndFonts.ps1 },$null)

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“ClearMenu”, 
  
{ $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear() }, $null)

 

When the Add-MenuItems.ps1 script runs, it creates a new top-level menu named Add-ons, and the additional menu items and submenu items.

The Restore menu consists of two submenu items. The first runs the RestoreAllDefaults command, and the second runs the RestoreTokenColorDefaults command. To access the AddOnsMenu, use the currentPowerShellTab property of the $psISE automatic variable. From the AddOnsMenu property, choose the Submenus property and call the Add method. It accepts three parameters. The first parameter is the name of the menu item. The second parameter is a script block that can run any Windows PowerShell command or call any Windows PowerShell script you wish to use. The third parameter is the keyboard shortcut. If you do not wish to specify a value for the parameter, you cannot leave it empty; therefore use the $null automatic variable. The code to create the Restore menu and its two submenus is shown here:

$RestoreRoot = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “Restore”
,$null,$null)
$RestoreRoot.Submenus.Add(“RestoreALLDefaults”, 
 
{$psISE.Options.RestoreDefaults()}, “Ctrl+Alt+R”)
$RestoreRoot.SubMenus.Add(“RestoreTokenColorDefaults”, 
 
{$psISE.Options.RestoreDefaultTokenColors()}, “Ctrl+Alt+T”)

This portion of the code creates the Restore menu item, as well as the two submenus shown in the following image.

Image of Restore menu and its two submenus

The GetOptions menu also consists of a menu and two submenus. This menu item calls two scripts, one of which displays the Windows PowerShell fonts and changes the Windows PowerShell ISE console to use that font. The second script displays the Windows PowerShell ISE colors that are available along with their name and hex value. Neither of these menu items assigns a keyboard shortcut, so the third position of the Add command contains a $null variable. This is shown here:

$GetOptions = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “GetOptions”
,$null,$null)
$GetOptions.SubMenus.Add(“GetFonts”, { C:\fso\Get-PsISEfonts.ps1 } , $null)
$GetOptions.SubMenus.Add(“GetColors”, { C:\fso\Get-PsIseColorValues.ps1 } , $null)

The GetOptions menu code produces the GetOptions menu system shown in the following image.

Image of GetOptions menu and its two submenus

The MyCustomISE menu item runs the Set-PsISEcolorsAndFonts.ps1 script. This portion of the script is shown here:

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(
   “MyCustomISE”
, { C:\fso\Set-PsISEcolorsAndFonts.ps1 },$null)

To clear the add-ons menu, call the Clear method from the Submenus object as shown here:

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“ClearMenu”, 
  
{ $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear() }, $null)

Well, that is about all there is to adding custom menu items to the Windows PowerShell ISE. Join us tomorrow as we continue to explore the Windows PowerShell ISE—it is way cool.

We would love you to follow us on Twitter and Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

0 comments

Comments are closed. Login to edit/delete your existing comments