Adding Custom Menu Items to the Windows PowerShell ISE

Adding Custom Menu Items to the Windows PowerShell ISE

  • Comments 4
  • Likes


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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • <p>Hi. This is cool stuff. &nbsp;But how come no links for Get-PSIseColorValues.ps1 and Set-IseColorsAndFonts.ps1? &nbsp;Are they available?</p>

  • <p>I&#39;m looking for the same thing, the links for those scripts or more information on creating it. &nbsp;Having some issues trying to create my own &quot;Get-PSIseColorValues.ps1&quot;.</p>

  • <p>sorry about that, I just put in the hyperlinks. </p>

  • <p>I am taking a class in scripting that involves powershell, I am attaching the project in hopes you or someone can help me as until now I have never worked in powershell and in the 90s barely had to use DOS. I hope you can help direct me as to how to do this.</p> <p>You are to develp a command script that displays a list of options to the user, prompts for input, and runs the function selected. &nbsp;You may use a batch script, a VBS script, or a PowerShell script. However be warned that the VBS and PowerShell version of this project can be very involved. The menu interface should include all of the following characteristics:</p> <p>· At command file startup, the color should be set to white text on a blue background and the screen should be cleared</p> <p>· The menu options should be visible until you exit the script (unless displaying an option)</p> <p>· A title for your utility should be displayed</p> <p>· The date and time should be displayed at the top of the menu directly after or under the title</p> <p>· The screen should be cleared before displaying the menu after commands are run</p> <p>The menu should include the following options:</p> <p>1. Show System Hostname</p> <p>2. Show System IP Addresses</p> <p>3. Show System DNS Servers</p> <p>4. Show System Default Gateway</p> <p>5. Show Amount of Memory Available to Run Applications</p> <p>6. Show PATH environment variable</p> <p>7. List Drivers Installed on System</p> <p>8. List current running tasks</p> <p>9. Kill task</p> <p>10. Display a text file on the screen</p> <p>11. Get File Security Information</p> <p>12. Show All TCP/IP Network Connections and listening Ports</p> <p>For items 9, 10 and 11, you must prompt the user for an entry to use in running the command.</p> <p>· You must display a prompt for the user to input an option</p> <p>· Output that runs more than the length of the command window should be paged with the user prompted to press a key to run the next screen</p> <p>· The user should be given time to examine the output of their selection before the menu is redisplayed</p>