Adding Functions to the PowerShell ISE Menu

One of the neatest features with the ISE is that you can add your own menu items. This particular feature allows you to add whatever kind of automation you want and make it available as both a menu item and a keyboard shortcut. In this tutorial, you will learn just how to add your own functions to the ISE menu. Let's get started!

Setup

If you have not already done so, click open Windows PowerShell ISE.

Step one.

In order to add whatever sort of automation you'd want both as a menu item and a keyboard shortcut, you must write your own function then access the $psISE variable's CustomMenu.Submenus collection using the Add method. To demonstrate this particular functionality, place this bit of code in your ISE profile script:

Example

Function My-Custom-Function 
{ 
Write-Host "Running my very own function!" 
} 
$psISE.CustomMenu.Submenus.Add("Run Custom Function", {My-Custom-Function), "Shift+Ctrl+f")
    

Copy and Try it

This code simply defines the function presented, called My-Custom-Function, which displays the text "Running my very own function!" in the output pane of the ISE. The $psISE.CustomMenu.SubMenus.Add method takes three. The first to name what you see in the menu, the second to define what you run and the third is the keyboard shortcut you want to assign to it. If no shortcut is desired, simply give it the $null value in place of where the shortcut would be.

The output will then write out to the output pane the string, Running my very own function!

Remember that if you are not permitted to make modifications to the system or are not the administrator, you will encounter the some problems receiving permissions to Access the registry key in order to remote sign the script or to simply get the script to automatically give itself permission every time its ran using:

Example


Set-ExecutionPolicy Unrestricted -Scope Process -Force


    

Copy and Try it

Remarks last but not least…

These functions have the ability to be saved for when PowerShell is being loaded as well. So as you see, there are many ways to customize and change the automation in your system. Just keep in mind that scripts and commands depending on how much authorization they need, might need a certificate to be fully ran. No worries, we will definitely get you up to speed on certifications, that's not for another couple of tutorials though. Join us next time for additional Windows PowerShell tutorials! Till then…