Prof. Powershell
ISE Made Easier
Some tips for customizing PowerShell's Integrated Scripting Environment.
- By Jeffery Hicks
- 05/23/2011
One of the benefits of the Windows PowerShell Integrated Scripting Environment is that is includes its own object model. You can explore this object in the ISE by looking at the $psise object:
PS C:\> $psise | format-list
CurrentPowerShellTab : Microsoft.PowerShell.Host.ISE.PowerShellTab
CurrentFile : Microsoft.PowerShell.Host.ISE.ISEFile
Options : Microsoft.PowerShell.Host.ISE.ISEOptions
PowerShellTabs : {PowerShell 1}
I don't have room to cover everything in one lesson, but today I want to show you how you can customize the ISE to add your own functionality and menu choices.
The ISE has a menu option called Add-Ons. You can use the ISE to add items to this menu. I have several in mine:
PS C:\> $psise.CurrentPowerShellTab.AddOnsMenu.Submenus | Select Displayname
DisplayName
-----------
IsePack
Test Buffer
Save File ASCII
Convert to text file
Add Header
Add Help
New Function
Insert Datetime
Open Current Script Folder
Most Recent
Let me explain how this works. To add an item to the menu you need a display name and an action. You can optionally define a keyboard short cut as well. The action can be any PowerShell command, scriptblock or function. For example, I have a command to insert the current date and time into the current script in the ISE. This is the command I run to add that menu item:
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Insert Datetime",{$psise.CurrentFile.Editor.InsertText($(get-date))},"ALT+F5") | out-Null
The Add() method needs the display name ("Insert DateTime"), a script block to execute and a keyboard short cut. I'm using Alt+F5. If you don't want to use one, use "" for the value.
My script block uses the InsertText() method of the $psise object to insert the result of the Get-Date cmdlet. I prefer to pipe the entire expression to Out-Null, otherwise I get a result in the ISE output pane that is irrelevant as far as I'm concerned.
You only need to run this command once per ISE session, so I put it in my ISE profile script which will be something like C:\Users\YOU\Documents\ WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1. Look at $profile in the ISE to see what it should be for you.
Here's another of my menu items.
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Open Current Script Folder",{Invoke-Item (split-path $psise.CurrentFile.fullpath)},"ALT+O") | out-Null
This opens the folder for the current script in Windows Explorer and has a keyboard shortcut of Alt+O.
Once you start creating your own add-ons for the ISE there's no limit to what you can add. I have a number of ISE-related functions on my blog that you might incorporate.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.