Skip to the main content.

ScriptRunner Blog

Five reasons you should be using PSReadLine

Table of contents

Post Featured Image

I'd like to think that because you are reading this, you are a professional PowerShell user and this article will be a review. But I know from experience teaching and presenting at conferences that I can't make any assumptions. While I think everyone already knows about PSReadLine, I know that's not the case. And even if you are using PSReadLine, are you using it to its full potential? 

Let me give you five reasons why you should be using PSReadLine daily in your PowerShell console work.

 

Get up to speed

The first thing you might need to do is verify you have the latest version of the PSReadLine module installed. But here's where it gets tricky. PSReadLine is included with Windows 10 and Windows Server 2016 and later. But the version that ships with Windows is not the latest. If you've never installed a newer version of the module, you are out of date. I believe PowerShell 7 ships with the latest version. But this is easy enough to check.

The PSReadLine module is available from the PowerShell Gallery. You can check the version you have installed with Find‑Module:

Find-Module PSReadLine

If you are using the newer PSResourceGet module run:

Find-PSResource PSReadLine

As of this writing, the latest version is 2.3.5. Check to see what version you have installed.

Get-Module PSReadLine -ListAvailable

Here's a Windows 10 system that has never been updated.

You must know the latest version you have because it will determine how you get the new version from the PowerShell Gallery installed. The issue is that PSReadLine ships with Windows which means you can't update with Update‑Module or Update‑PSResource.

If you get an error like this you need to install the module.


Install-Module PSReadline -Scope AllUsers -Force

I prefer to install this module for all users. Once installed, restart your PowerShell session and use Get‑Module to verify the version.

If you have older versions installed, the Update‑Module or Update‑PSResource commands should work.

You should also run Update‑Help PSReadline -force after installing or updating PSReadLine. If you get an error in Windows PowerShell 5.1 that help can't be found, you may need to make a minor fix to address a casing issue. Originally, the module was PSReadline with a lowercase 'l'. But it is now PSReadLine with an uppercase 'L'. If you encounter this problem, follow the instructions from Sean Wheeler on the PowerShell team blog.

 

Command completion

You are probably familiar with tab completion in PowerShell. The PSReadLine module is what makes this possible. You can get tab completion for command and parameter names. Begin typing a command like Get‑Win and press the tab key. PowerShell will complete the command name, Get‑WinEvent, for you. If there are multiple matches press the tab key to cycle through them. Press Shift‑Tab to cycle backward. Once you have the cmdlet name, press -l and tab to cycle through all the parameters that begin with l. Assuming you want -ListLog, all you need to do is enter the parameter value like *. With only a few keystrokes, you have a complete command.

It gets better. Let's say you don't recall the parameters. Type the command name followed by a dash and then press Ctr+Space. PSReadLine will display a list of all available parameters.

Use the arrow key to select the parameter and press Enter. Repeat until you have all the parameters you need. PSReadLine is also smart enough to detect parameter sets. If you start a command with a parameter that belongs to a specific parameter set, Ctrl+Space will only show you compatible parameters.

 

Command prediction

If you liked PSReadline predicting what parameter to use, you'll love command prediction. This feature is based on the idea that you often want to re-run commands in your PowerShell session. This feature makes it easy to do just that. It should be enabled by default.

PSReadLine will save command history to a text file. This allows your command history to persist between PowerShell sessions. The text file is stored under your AppData folder and will not store sensitive information. As long as you don't goof and enter a password in plain text at a prompt. You can view the related settings with Get‑PSReadLineOption.

The history log is a text file so you can easily edit it if you need to. You can use Set‑PSReadeLineOption if you want to change the location.

Here's how PSReadLine uses your history file. Start typing a command that you've used earlier in your session. PSReadLine will show you a possible completion for the command based on your history.

It might be hard to see with the default settings.

You can probably barely read the predicted text. You will most likely want to change it.


Set‑PSReadLineOption -Color @{InLinePrediction = "$([char]27)[3;92m"}

You can use any ANSI escape sequence or $PSStyle. I've chosen green italics. This change only lasts for the duration of your PowerShell session. Put the command in your PowerShell profile script to configure your session. But, now the prediction is much easier to see.

If this is the command you want, press the right arrow key to accept the prediction. The inline prediction shows the most recent command. But check what happens when you press F2.

Use the arrow keys to select the command you want and press Enter. Be careful because command history will also include commands that failed.

You can press F2 and any time to toggle between inline and list views. If you want to default to the list view add this line to your PowerShell profile script:

Set‑PSReadLineOption -PredictionViewStyle ListView

 

Inline help

This next reason to use PSReadLine was recently added. That's why I want to make sure you are using the latest version of the module. I'm sure this has happened to you. You start typing a command like this:


PS C:\> Get-Acl _

But you can't remember the command details. Yes, you could use PSReadline to complete parameters, but you want to read the help. Leaving the command where it is press F1. PSReadLine will display full help for the command.

Scroll through and read the help. When finished, press Q and you are back to your command line exactly where you left off. Use tab completion to finish the command. If you have a pipelined expression, move your cursor to any command and press F1 to get help for that command. This feature is a real time-saver.

 

Key handlers

The last reason to get into the PSReadLine habit is to take advantage of key handlers. These are special key bindings that make it easier to manipulate your command line. PSReadLine ships with many default keybindings. Run Get‑PSReadLineKeyHandler to see what has been defined.

You can define your own. You can set key handlers for native PSReadline functions. I have these commands in my profile script. You can also use the alias Key in place of Chord.


Set-PSReadLineKeyHandler -Function capitalizeword -Chord 'alt+c'
Set-PSReadLineKeyHandler -Function upcaseword -Chord 'alt+u'
Set-PSReadLineKeyHandler -Function downcaseword -Chord 'alt+l'

I can type a word, move the cursor to the beginning of the word, and use any of the key combinations.

You might want to run Get‑PSReadLineKeyHandler | sort key | select key,function to see what keys are already defined. You also might need to watch our for conflicts with Windows Terminals or VS Code.

You can define key handlers to do just about anything.


Set-PSReadLineKeyHandler -key "alt+n" -ScriptBlock {notepad}

Pressing Alt+n at the console will open Notepad. The key handlers are normally used to process the current line, but you can get creative.


Set-PSReadLineKeyHandler -key "alt+t" -ScriptBlock { Get-Date -Format "hh\:mm\:ss"|
Write-Host -ForegroundColor
Green}

Now I can press Alt+t to display the current time in green at my prompt. I will be the first to admit that key handlers can get very complicated.


Set-PSReadLineKeyHandler -key F6 -BriefDescription 'Function Menu' -Description "Display all functions as menu using Out-GridView. [$($env:username)]" -ScriptBlock {
$line = $null
$cursor = $null
[Microsoft.PowerShell.PSConsoleReadLine]::GetBufferState([ref]$line, [ref]$cursor)

#filter out the built-in functions for changing drives and a few others
Get-ChildItem -Path function: |
Where-Object { $_.name -NotMatch '([A-Za-z]:)|(Get-Verb)|(prompt)|(cd)|(clearhost)|(more)|(pause)' } |
Select-Object Name, Version, Source, @{Name = 'Syntax'; Expression = { (GetCommand $_.name -Syntax | Out-String).Trim() } } |
Out-GridView -Title 'Function Menu: Select one to run' -OutputMode Single |
ForEach-Object { Show-Command -Name $_.name }
}

This key handler will display a menu of all functions in your session. Select one and press Enter. The function will be displayed in the Show-Command window. You can then run the function. This is a great way to explore functions in your session. If you search for PSReadLine key handlers, you'll find many examples. Put the ones you want to use all the time in your profile script. Don't feel obligated to create complex key handlers.

You can learn more by reading the Microsoft documentation.

 

Your turn

I hope I've given you five good reasons to start using PSReadLine if you haven't already. If you are, I hope I've given you some ideas on how to use it more effectively. PSReadLine is a powerful tool that can make you more productive in your PowerShell console work.

 

Good2know

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.

The poster is available for download and in paper form.

PowerShell Poster 2023

Get your poster here!

 

 

Related links

Related posts

8 min read

Scriptember 2024 – Celebration of PowerShell and its community

Welcome to Scriptember! We are thrilled to announce the launch of a unique, month-long campaign dedicated to...

10 min read

Five reasons you should be using PSReadLine

I'd like to think that because you are reading this, you are a professional PowerShell user and this article will be a...

8 min read

PowerShell 7: What are the pros and cons of switching from Windows PowerShell 5.1?

Windows PowerShell 5.1 is usually preinstalled and the default – is it worth switching to PowerShell 7, or will it lead...

About the author: