Friday Fun A Graphical PowerShell History Picker

One of my favorite features in PowerShell 3.0 is that you can select items in Out-Gridview which will then pipe the object back to the pipeline. One way I’ve been using this is as graphical “picker” for command history. I use Get-History, actually its alias h, all the time. Once I know the history number I then use Invoke-History, or its alias r. Now, with Out-Gridview I can sneak in a little something extra.

PS C:\> h | ogv -p | r

I realize this is cryptic but when using the shell interactively efficiency is paramount. This is the actual full command.

PS C:\> Get-History | Out-Gridview -passthru | Invoke-History

I’ll get something like this:


I can select an item, click OK and the command will run back in my console. There are a few downsides, but remember this is a Friday Fun article. First, if you cancel, PowerShell will invoke the last command again. Also, as written if you select multiple items, which you could with -Passthru, you’ll get an error because Invoke-History won’t accept multiple entries. You could force Out-Gridview to only allow a single selection.

PS C:\> h | ogv -OutputMode Single | r

Or you could use Foreach to handle multiple selections.

PS C:\> h | ogv -p | %{r $_}

Be aware that when you run multiple commands in the same pipeline formatting can get a little screwy so I would probably stay way from it.

One more way you might use this is with Invoke-Expression instead of Invoke-History. The history object has a commandline property which you could invoke.

PS C:\> (h | select * | ogv -p).Commandline | iex

This will also handle multiple commands. If you cancel from Out-Gridview, Invoke-Expression will throw an exception but at least it won’t try to re-run the last command which is probably better.

Again, these are all “quick and dirty” commands to have fun with from a PowerShell prompt.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

About the Author Announcer

This is the official account for and sponsor announcements.