PSReadLine is a module created by Microsoft to customize the command line editing environment in PowerShell. It offers numerous customizations that can change how your command line editor presents data in many ways.

PSReadLine v1.x was originally introduced for Windows 5.x PowerShell. It allowed users to access their history easily and also provided tweaks such as syntax highlighting at the PowerShell command prompt. Users were very fond of the original iteration, but for a long time, no changes were introduced. Microsoft was working on a total rewrite of the PSReadLine v2 module to work with the new cross-platform version of PowerShell. The rewrite was an arduous and time-consuming process, but it's now complete.

The new 2.x version offers many improvements. Today, I'll show you some examples of many options that you can configure. You can read about all the options available at Microsoft's GitHub page for the module. Installing the module does not differ from any other module if you're using PowerShell v7.

Install-Module PSReadLine

If you are still using Windows PowerShell 5.x, you will need to install the latest version of the PowerShellGet module to install the PSReadLine module.

After installation, you can add customizations to your profile to change the behavior of the module. Before you make any changes, you'll probably notice that PSReadLine automatically adds color to syntax in ways that make things easier to read. In the screenshot below, you can see the colors used for variables, parameters, and cmdlet names.

PSReadLine syntax coloring defaults

PSReadLine syntax coloring defaults

The first thing to check out is the default key bindings set after installation. There is an extensive list of options that are configured by default. This screenshot is about a quarter of the full list.

Get-PSReadlineKeyHandler
PSReadLine keyhandler options

PSReadLine keyhandler options

Configuring history options ^

One of the best features of this module is its ability to search automatically through your history of commands. However, that functionality isn't enabled by default. Enabling it is a simple process, and you can change a few options to tailor it to your preference.

The Up and Down arrows are the first thing you should set to browse history from the command prompt.

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

Let's move on to some of the more interesting options. Let's set up history matching. Enabling this option will show matches of previous commands as you type.

Set-PSReadLineOption -PredictionSource History

Once enabled, you should start to see matches as you type.

Matching on previous history

Matching on previous history

I have underlined the part I typed and the part that Microsoft is matching on. Once you have some matching text, you can use the right arrow key to accept the text and skip having to type the rest of the line.

However, if you're like me, you might think that the default dark gray text is impossibly hard to read. We can change that. Below, I set color options for the typed text and the matched text. The colors can be hexadecimal, RGB, or console colors. In this example, I chose red for matched text and magenta for prediction match text.

Set-PSReadLineOption -Colors @{emphasis = '#ff0000'; inlinePrediction = 'magenta'}
Changing colors for history matches

Changing colors for history matches

Another example:

Set-PSReadLineOption -Colors @{emphasis = '#ff0000'; inlinePrediction = 'Gray'}
An alternative color configuration

An alternative color configuration

You may have noticed that I said I picked red for emphasis, but you see no red text. That's because PSReadLine default coloring is kicking in. When you use the Up arrow for matches, the color becomes visible.

Red text highlights previous history matches

Red text highlights previous history matches

Some people love this setting. There is another setting I prefer that changes the match output. Previous searches showed inline matches. However, I prefer the list view setting, which is set by pressing the F2 key at the command line.

List view of history matches

List view of history matches

Other options you may want to consider that are self-explanatory are history deduping and tab completion.

Set-PSReadLineOption -HistoryNoDuplicates
Set-PSReadLineKeyHandler -Key Tab -Function Complete

AZ Predictor for AI-based predictions ^

PSReadLine also has support for Azure's ability to predict what you are going to type. The Azure module has over 4000 cmdlets, each of which has about 10 parameters. Using your history file is great, but at some point, you won't have any history for something you want to type. Having a tool that can make valuable suggestions as you type is very useful.

Microsoft has been working on the ability to use AI to make educated guesses or helpful predictions. It took some time, and the work is bearing fruit. I'll show you an example shortly, but first let me detail the changes you need to enable to get this functionality.

As of this writing, the current version of PSReadLine is v2.2.2. You need to use PSReadLine v2.1.0 or higher for the AI functionality to be supported. Next, you need to tell PSReadLine to check the history and use the AZ Predictor module.

Set-PSReadLineOption -PredictionSource HistoryAndPlugin

You also need to install an add-on module called AZ Predictor.

Install-Module -Name Az.Tools.Predictor

After installing the extra module, you need to enable the AZPredictor service.

Enable-AzPredictor -AllSession

These extra steps are worth the effort, though, as you will soon see. After we set everything up, the predictor will kick in and make suggestions when you type syntax that matches the AZ cmdlets.

History and AZ Predictor module suggestions

History and AZ Predictor module suggestions

In this example, I typed Get-AzAD, which I often do since I do AzureAD management. You can see that it pulled up a few matches from my history. But now we see that the AZ Predictor module is also offering suggestions. As you type more text, the predictions become more accurate. I will tell you it is a little scary how good the predictions are. Sometimes, it knows nearly the exact syntax I was going to type with only a few characters.

So far, I have been focusing mostly on history settings. There are many other things that the module can do. For example, you can configure the module to complete a quote if you type a single or double quote character. You type one, and it will automatically add the second.

You can also configure the module to do screen captures with a key combo. Another option is to replace aliases in your lines of code on the fly.

Microsoft provides a sample file that has some interesting examples you should look at.

Making your settings permanent ^

One thing I haven't covered yet is that all the settings I detailed previously will be gone once you close your session. You don't want to be forced to configure color and history options every time you open your shell. You can avoid this by adding the configurations you want to your profile. Then, every time you launch your shell, your preferences will be in place and ready to go.

Using this module has changed the way I work, and I couldn't imagine not having it available to me after using it for over a year now in my production workflow. I would suggest that you give the module a try to see how it can help you get through the simple stuff much faster and more easily.

If you want more information on the PSReadLine module, check out these sources. Visit the Microsoft docs website, which explains the module. There's more information at the GitHub repo for the module as well.

Subscribe to 4sysops newsletter!

I hope you enjoyed this look at the PSReadLine module. It has many features to explore. I have covered some of the most popular features. There's more to discover, and it is worth your time to explore the options.

2 Comments
  1. Alexander 4 months ago

    Thanks for the information! I didn’t know about some features.

  2. Em 4 months ago

    I have been using PSReadLine for quite some time, and during the PS Unplugged I was impressed with the new features upcoming in version 2.2.2.
    I upgraded last week when it was released, but thanks to your article I can take advantage of even more features in it. Great explanations, Mike. Cheers for the explanations.

Leave a reply

Your email address will not be published.

*

© 4sysops 2006 - 2022

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account