Saturday, November 12, 2011

Using the PowerShell ISE–two cool finds

I’ve been using PowerShell’s Integrated Scripting Environment ever since it was in beta. Despite being relatively slow to load, it sure beats using Notepad for simple script development/debugging. The colour coding alone makes it even more useful! I love how you can add functionality to the editor via object model. It’s neat how that object model is exposed inside PowerShell as the $ISE variable – and how easy it is to use it to add menu items (and keyboard short cuts for the menu items).

A freely available module that offers a lot of ISE customisation is the IsePack, which is part of the PowerShellPack mega-module issued by Microsoft as part of the Windows 7 Resource Kit. You can download the full PowerShellPack from http://archive.msdn.microsoft.com/PowerShellPack. The PowerShellPack module is actually a set of 10 sub-modules. You can import the entire module (Import-Module PowerShellPack) or the sub-components. In my ISE Profile file, I add the ISE pack in specifically (Import-Module IsePack). That in turn exposes me an additional Add-On menu item, ISEPACK. This menu contains a set of sub-menus which add a lot of features to the ISE.

My two cool finds are partly what’s in IsePack and what I could easily add to it. The ISEPack creates a number of functions based on the ISE’s object model. It then leverages those in additional functions that PowerShell associates with menu items and keyboard shortcuts when the module is imported. And since the module is just a text file – you can easily edit it and add more features.

The first cool find is the Search-Bing function, which is associated with a menu item (Add-ons/IsePack/Search-Bing) and a keyboard shortcut (Ctrl-B). The Search-Bing function uses a lower level function,  Select-CurrentText, that gets the text that is currently selected somewhere on the ISE', then pipes it to copy of IE which points http://www.bing.com/search?q=$_. Thus, if I have the text ‘ToString’ highlighed in an open editor window, and hit Ctrl, I get a Bing Search Page, like this:

image

 

The second cool thing was how easy it was to ammend the IsePack module to add a Search-Google function. In the IsePack.PSM1 file, there’s a fragment of code that implements the Search-Bing feature (and adds it to the menu) This fragement is part of a larger script, but here’s the starting poing:

"Search-Bing" = {
        $Shell = New-Object -ComObject Shell.Application
        Select-CurrentText | Where-Object { $_ } | ForEach-Object {
            $shell.ShellExecute("
http://www.bing.com/search?q=$_")
        }
    } | Add-Member NoteProperty ShortcutKey "CTRL+B" –PassThru

 

As you can see, Search-Bing is associated with a script block that first opens the currently configured Web Browser (FireFox in my case). Then it executes a Bing Search on the currently selected text. This results in the search window coming up. So how hard was it to add to add a Search-Google? Trivial as it turns out. I just added the following text directly below the Search-Bing definition:

    "Search-Google" = {
        $Shell = New-Object -ComObject Shell.Application
        Select-CurrentText | Where-Object { $_ } | ForEach-Object {
            $shell.ShellExecute("http://www.Google.com/search?q=$_")
        }
    } | Add-Member NoteProperty ShortcutKey "CTRL+Shift+G" -PassThru

 

Sadly, Ctrl+G was already taken by something (a puzzle for another day!), so I used Ctrl+Shift+G and that brings up something like this:

image

 

As I am currently working on my next Pluralsight course on Formatting with PowerShell, I’m finding a need to look stuff up in MSDN and these two functions sure are useful to me!

1 comment:

Rafael Spessotto said...

Hi Thomas! Could u help me with some simple samples for telephony? Im new on telephony software, and im trying to learn, because i want to do a call center software..but im very new in this area..i saw a comment in microsoft site, so, if u could, please send me a email rafa_spe@hotmail.com...thank so much! Rafael