PSReadline, a very handy module

PSReadline is something I use a lot, and starting from Windows 10 (And on PowerShell 6.x and higher), you probably do too! But you can customize it a bit to use more of its features. In this blog post, I will show you a few examples.

Upgrade it to the latest version first

Check which version you’re currently having running by using:

Get-Module PSReadline

You should get output like this which the currently installed version:

To update it to the latest version, you can run this command:

Install-Module PSReadline -Force

After restarting PowerShell (Or Windows Terminal), it should give you this output when checking the version:

Adding options to your PowerShell profile

After you have updated to the latest version, you can start adding options to your PowerShell profile so that they will be active when starting a new PowerShell session. You can edit your profile by running:

Notepad $Profile

I have these two configuration options present in my profile:

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadLineOption -PredictionSource History

But what do these options do precisely?

Set-PSReadlineKeyHandler

This cmdlet lets you configure a certain key, in this case, the TAB key, to use a certain function when pressed. The option that I use is MenuComplete. Below is a screenshot of what this option does.

When you type ‘-‘ and press Tab, it typically lets you cycle through all the parameter options. The MenuComplete option will show you all the parameters below your command line, and you can browse all of them instead of cycling through them, pressing Tab multiple times.

Set-PSReadlineOption

The other PSReadline option that I use is the PredictionSource parameter which nicely autocompletes from your History if you use a specific command a lot… It will allow you to press the -> key to complete the line, and you can just press Enter to run it. For example:

In this case, I typed the letter ‘w’ and nothing else. Since I like to update all my software running Winget, it suggests the whole command line for me to upgrade all software silently. It saves a lot of typing 🙂

List of PSReadlineKeyHandler key-bindings

You can check all the key bindings available by running:

Get-PSReadLineKeyHandler

There are many options available. I use CTRL+R (Search history backward interactively) to search my previous commands easily.

Basic editing functions

Key Function Description
— ——– ———–
Enter AcceptLine Accept the input or move to the next line if the input is missing a closing token.
Shift+Enter AddLine Move the cursor to the next line without attempting to execute the input
Backspace BackwardDeleteChar Delete the character before the cursor
Ctrl+h BackwardDeleteChar Delete the character before the cursor
Ctrl+Home BackwardDeleteInput Delete text from the cursor to the start of the input
Ctrl+Backspace BackwardKillWord Move the text from the start of the current or previous word to the cursor to the kill ring
Ctrl+w BackwardKillWord Move the text from the start of the current or previous word to the cursor to the kill ring
Ctrl+C Copy Copy the selected region to the system clipboard. If no region is selected, copy the whole line
Ctrl+c CopyOrCancelLine Either copy selected text to the clipboard, or if no text is selected, cancel editing the line with CancelLine.
Ctrl+x Cut Delete selected region placing deleted text in the system clipboard
Delete DeleteChar Delete the character under the cursor
Ctrl+End ForwardDeleteInput Delete text from the cursor to the end of the input
Ctrl+Enter InsertLineAbove Inserts a new empty line above the current line without attempting to execute the input
Shift+Ctrl+Enter InsertLineBelow Inserts a new empty line below the current line without attempting to execute the input
Alt+d KillWord Move the text from the cursor to the end of the current or next word to the kill ring
Ctrl+Delete KillWord Move the text from the cursor to the end of the current or next word to the kill ring
Ctrl+v Paste Paste text from the system clipboard
Shift+Insert Paste Paste text from the system clipboard
Ctrl+y Redo Redo an undo
Escape RevertLine Equivalent to undo all edits (clears the line except lines imported from history)
Ctrl+z Undo Undo a previous edit
Alt+. YankLastArg Copy the text of the last argument to the input

Cursor movement functions

Key Function Description
— ——– ———–
LeftArrow BackwardChar Move the cursor back one character
Ctrl+LeftArrow BackwardWord Move the cursor to the beginning of the current or previous word
Home BeginningOfLine Move the cursor to the beginning of the line
End EndOfLine Move the cursor to the end of the line
RightArrow ForwardChar Move the cursor forward one character
Ctrl+] GotoBrace Go to matching brace
Ctrl+RightArrow NextWord Move the cursor forward to the start of the next word

History functions

Key Function Description
— ——– ———–
Alt+F7 ClearHistory Remove all items from the command line history (not PowerShell history)
Ctrl+s ForwardSearchHistory Search history forward interactively
F8 HistorySearchBackward Search for the previous item in the history that starts with the current input – like Previous History if the input is empty
Shift+F8 HistorySearchForward Search for the next item in the history that starts with the current input – like NextHistory if the input is empty
DownArrow NextHistory Replace the input with the next item in the history
UpArrow PreviousHistory Replace the input with the previous item in the history
Ctrl+r ReverseSearchHistory Search history backward interactively

Completion functions

Key Function Description
— ——– ———–
Tab Complete Complete the input if there is a single completion, otherwise complete the input with a common prefix for all completions. Show possible completions if pressed a second time.
Ctrl+@ MenuComplete Complete the input if there is a single completion, otherwise complete the input by selecting from a menu of possible completions.
Ctrl+Spacebar MenuComplete Complete the input if there is a single completion, otherwise complete the input by selecting from a menu of possible completions.
Shift+Tab TabCompletePrevious Complete the input using the previous completion

Prediction functions

Key Function Description
— ——– ———–
F2 SwitchPredictionView Switch between the inline and list prediction views.

Miscellaneous functions

Key Function Description
— ——– ———–
Ctrl+l ClearScreen Clear the screen and redraw the current line at the top of the screen
Alt+0 DigitArgument Start or accumulate a numeric argument to other functions
Alt+1 DigitArgument Start or accumulate a numeric argument to other functions
Alt+2 DigitArgument Start or accumulate a numeric argument to other functions
Alt+3 DigitArgument Start or accumulate a numeric argument to other functions
Alt+4 DigitArgument Start or accumulate a numeric argument to other functions
Alt+5 DigitArgument Start or accumulate a numeric argument to other functions
Alt+6 DigitArgument Start or accumulate a numeric argument to other functions
Alt+7 DigitArgument Start or accumulate a numeric argument to other functions
Alt+8 DigitArgument Start or accumulate a numeric argument to other functions
Alt+9 DigitArgument Start or accumulate a numeric argument to other functions
Alt+- DigitArgument Start or accumulate a numeric argument to other functions
PageDown ScrollDisplayDown Scroll the display down one screen
Ctrl+PageDown ScrollDisplayDownLine Scroll the display down one line
PageUp ScrollDisplayUp Scroll the display up one screen
Ctrl+PageUp ScrollDisplayUpLine Scroll the display up one line
F1 ShowCommandHelp Shows help for the command at the cursor in an alternate screen buffer.
Ctrl+Alt+? ShowKeyBindings Show all key bindings
Alt+h ShowParameterHelp Shows help for the parameter at the cursor.
Alt+? WhatIsKey Show the key binding for the next chord entered

Selection functions

Key Function Description
— ——– ———–
Ctrl+a SelectAll Select the entire line. Moves the cursor to the end of the line
Shift+LeftArrow SelectBackwardChar Adjust the current selection to include the previous character
Shift+Home SelectBackwardsLine Adjust the current selection to include from the cursor to the start of the line
Shift+Ctrl+LeftArrow SelectBackwardWord Adjust the current selection to include the previous word
Alt+a SelectCommandArgument Make a visual selection of the command arguments.
Shift+RightArrow SelectForwardChar Adjust the current selection to include the next character
Shift+End SelectLine Adjust the current selection to include from the cursor to the end of the line
Shift+Ctrl+RightArrow SelectNextWord Adjust the current selection to include the next word

Search functions

Key Function Description
— ——– ———–
F3 CharacterSearch Read a character and move the cursor to the next occurrence of that character
Shift+F3 CharacterSearchBackward Read a character and move the cursor to the previous occurrence of that character

List of PSReadlineOption parameters

You can get a list of all configurable options which you can add to your PowerShell Profile by running:

Get-PSReadLineOption

This gives you the following output containing the options and how there are configured now:

You can configure a lot to your liking, for example: If you don’t like the ‘Ding’ sound it makes when you make type/use a character that’s not available, you can use:

Set-PSReadLineOption -BellStyle:None

2 thoughts on “PSReadline, a very handy module

  1. Pingback: EndPoint Manager Newsletter – 28th April 2022 – Andrew Taylor

  2. Pingback: Using Windows Terminal for PowerShell | PowerShell is fun :)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.