Archives for the Month of September, 2012

Bash-like Tab Completion in PowerShell

[Edit 10/2/2015] - PowerShell 5.0 now includes a new and amazing PSReadLine module by default. You can also install it from here: https://github.com/lzybkr/PSReadLine.

 

When PowerShell first came on the scene, Unix folks were among the first to notice. They’d enjoyed a powerful shell and a vigorous heritage of automation for years – and “when I’m forced to use Windows, PowerShell rocks” was a phrase we heard many times.

This natural uptake was no mistake. There are many on the team that come from a deep Unix background, and similarities to traditional Unix shells were no mistake.

When coming from a Unix background, through, we still hear the occasional grumble that tab completion feels weird. Ctrl-R doesn’t invoke history search? Tab cycles through matches, rather than list them? Abhorrent!

In PowerShell V1 and V2, there was nothing you could reasonably do to address this. PowerShell reads its input from the console in what is known as “Cooked Mode” – where the Windows Console subsystem handles all the keypresses, fancy F7 menus, and more. When you press ENTER or TAB, PowerShell got the text of what you had typed so far, but that’s it. There was no way to know that you had pressed Ctrl-R, Ctrl-A, Ctrl-E, etc.

We resolved this issue in PowerShell version three. Windows does, of course, offer a “Raw Mode” for user input. While we wouldn’t want to replace PowerShell’s default ReadLine() implementation with that of Bash, some enterprising soul might want to. To support this, we introduced the PSConsoleHostReadLine function in PowerShell version three. If this function exists, we call that instead of the Cooked Mode ReadLine(). And that’s it – the rest is up to you.

It turns out that some enterprising soul does exist, and he’s in fact one of our MVPs: Oisin Grehan. By building on the ReadLine() implementation in Mono, he’s developed a module that offers Bash-style line editing in PowerShell.

Have at ‘er! http://nivot.org/nivot2/post/2012/09/12/Emulating-Bash-GNU-Readline-with-PowerShell-30.aspx

Scripts in the PowerShell Cookbook?

The third edition of the PowerShell Cookbook is getting close! We should have an online preview ready for your perusal in the near future – stay tuned for an update.

One “problem” we’re running into is its size. This is where we’d love some feedback.

We could make it thinner and cheaper by not printing the scripts, but instead hosting them online. That would mean, of course, that you can’t just sit back and read through the sample code.

So a question, dear reader.

If you own a copy of either the first or second edition of the PowerShell Cookbook, did you find value with having them included in the book itself? Or would a download provide just as much benefit?