Back in the days of PowerShell v4, a member of the PowerShell team put together a module to make it easier to work with the console shell and to bring in some functionality from other shell experiences like bash. The module, PSReadline, was modeled after the GNU Readline library. The library offered a suite of functions and commands to enhance console based sessions. When WIndows 10 shipped with PowerShell 5.0, PSReadline was included by default. You will also find it in the next generation of PowerShell that runs on Linux and MacOS. I thought it would be helpful to give you some tips on getting started using the features and commands of this module.

Command Completion

PowerShell has long had tab completion for command and parameters, which you really need to get in the habit of using. PSReadline takes this a step further. Start typing a command like Get-Eventlog and then a dash to indicate the beginning of a parameter. Then hit Ctrl+Space and PSReadline will display all appropriate parameters.

PSReadline Parameter Completion (Image Credit: Jeff Hicks)

Use the arrow keys to select the parameter and press the space bar to insert it. Repeat the process to insert additional parameters. PSReadline will also display possible values, depending on the parameter.

Listing Possible Parameter Values (Image Credit: Jeff Hicks)

If the enumeration will be large, PSReadline will prompt you:

PSReadline Enumeration Confirmation (Image Credit: Jeff Hicks)

Although, you can be smart and limit the choices.

Fine Tuning the Enumeration Request (Image Credit: Jeff Hicks)

Command Editing

PSReadline also offers a number of keyboard commands that make it easy to edit a long command. For example, you can use HOME and END to jump to the beginning or end of a command. Or use Ctrl+LeftArrow and Ctrl+RightArrow to jump from word to word. If you have a command with { }, you can position your cursor on one of the braces and use Ctrl+} to jump to the matching brace. This also works with parentheses. This is very handy with nested expressions.

If you are creating a long expression in the console, you can also insert and delete lines. Suppose I have started something like this:

A Sample PowerShell Expression (Image Credit: Jeff Hicks)

I can use the keyboard navigation keys to easily move around. I can move the cursor up to the Where-Object command and hit Ctrl+Enter to insert a new line.

Inserting a New Line (Image Credit: Jeff Hicks)

All of the editing shortcuts are documented in about_psreadline,

Command History

The last feature I want to cover today is how PSReadline handles command history. The module maintains command history outside of your session and makes it available in other sessions. In my current PowerShell session, I wrote the string “banana” to the pipeline. I then opened a second PowerShell session. Get-History shows no commands. But if I press the up arrow, I get the last command, “banana” from the other session! Although commands from the new session do not flow “backward” to the other session. But if I open a third session, I get command history from the previous two sessions. The best part is that even when I close all my PowerShell sessions, and open a new one, pressing an up arrow gets my command history from the previous sessions. By default, PSReadline will save 2048 commands. I will show you how to modify that in another article.

Subscribe to Petri Newsletters Office 365 Insider Our Petri Office 365 Insider is dedicated to sharing detailed knowledge from top Office 365 experts. Delivered once a month to your inbox. All Newsletters Petri.com may use your contact information to provide updates, offers and resources that may be of interest to you. You can unsubscribe at any time. To learn more about how we manage your data, you can read our Privacy Policy and Terms of Service. !Already a Petri.com member? Login here for 1-click registration.

The documentation says that PSReadline does not work in the PowerShell ISE. Although, you will not get any errors if you import the module. The same is true of the integrated PowerShell console in VS Code.

There is much more to talk about, so I will pick up again in my next article. In the mean time, I strongly encourage you to read the about_psreadline help topic and start trying things out for yourself.