Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 26th at 1 PM ET!

Getting Started with PSReadline -- Part 1

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.



Sponsored Content

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

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)
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)
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)
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)
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)
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)
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.

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.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.


Sponsored By