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.
I realize this is cryptic but when using the shell interactively efficiency is paramount. This is the actual full command.
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.
Or you could use Foreach to handle multiple selections.
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.
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.