Friday, March 6, 2009

Add Custom Menu's to the Powershell ISE via Powershell Profile

Lately I have been exploring some of the Powershell 2 CTP3 features. The features that I am studying right now are, Advanced Functions (Very Useful!), WPF & .NET programming via Powershell (The Add-Type CMDlet), and dabbling with the ISE (Integrated Scripting Environment) that comes with Powershell 2.0. Today I have been playing with adding custom menus to the ISE. I wanted to add a custom menu to my powershell profile so when I start up the ISE it will be there by default. The problem is if you add the custom menu entry for the ISE to your powershell profile, it will create an error for the CLI or default version of powershell.

$psISE.CustomMenu.Submenus.Add("Dir", {Dir}, "f7")

I know there's probably hundreds of ways the do this but... I used an if statement to get rid of the error.

[string]$ISE = $psISE
if($ISE -match 'System.Management.Automation') {$psISE.CustomMenu.Submenus.Add("Dir", {Dir}, "f7")}

After adding this to my powershell all I have to do is hit F7 to run a DIR command. User your Imagination, You can probably come up with a better use for this than just a DIR

May The PoSH Be With You
Powershell Jedi