PowerShell Pipeline

Enhance Your PowerShell Experience Using PSReadLine

This console editing feature has quite a few handy uses.

If you've been using PowerShell since the days of V1 and V2 (and even V3), you are very familiar with the console and its lack of console editing features and maneuverability. All of that changes with the PSReadLine module that is available to download and use on PowerShell V3 through 5. This wonderful module comes to us via Jason Shirk of the PowerShell team and will greatly change the way that you use the PowerShell console!

If you want to install this amazing module, you can either install it from the PowerShell Gallery using Install-Module.

Install-Module -Name PSReadLine 

If you are running V3 or 4 of PowerShell, just head over to the GitHub site, download it and install to your system. It should go without saying that this should have a permanent place in your PowerShell profile so it is already loaded up as soon as you open up the console. Keep in mind that this is only useful in the console, not in the ISE.

As soon as you import the module, you will be greeted with syntax highlighting whenever you start typing out a command.

[Click on image for larger view.]  Figure 1. Showing off the syntax highlighting of PSReadLine

Not only do you get that, but you can quickly tell if there is an error in the syntax of a command by looking at the ">" at the end of the prompt.

[Click on image for larger view.]  Figure 2. Error in syntax while typing command.

In fact, if you decide to move forward with that error occurring, you will still get a second chance to not run the command by displaying some error text below the command. You can choose to continue and bring on the errors or you can go back and edit the command to save face.

[Click on image for larger view.]  Figure 3. Imminent error approaching if you run this command!

How many times have you been writing a command in the console and wish that you could just go down to another line just so the code is more readable? Guess what? You have this capability simply by using Shift+Enter

[Click on image for larger view.]  Figure 4. Multi-row editing.

Another nice feature is that if you want to run through the previous command history using the up arrow, if you had a multi-line command that was run previously, you will actually get the whole command, not just a single line from each part of the command. I don't know about you, but this is much nicer than trying to tread along and ensuring that I have captured every single line from a multi-line command in hopes that it will work and not worry about experiencing the frustration when I am halfway into the command and forgot a line.

Something that I haven't mentioned yet is the sorely needed Copy/Cut/Paste commands that are now available to use. Ctrl+C, Ctrl+X and Ctrl+V are now available for you to use without fear of that awkward ^V that shows up if you tried to do this before. How does this happen? Well, there are PSReadLine key handlers which allow you to map a command to a key that can be used whenever you need it.

Adding your own custom KeyHandlers is as simple as using the Set-PSReadlineKeyHandler command and specifying an action to perform. Want a quick way to open up your PowerShell profile in ISE? Just use this:

Set-PSReadlineKeyHandler -Key Ctrl+P -BriefDescription "Edit  PowerShell profile in ISE" -ScriptBlock  {
ISE $Profile

Ok, that one is simple. How about something a little more complex? This key handler will go through all of the aliases in your command and expand them out to their full command.  Note: This example comes courtesy of the examples page on the PSReadLine github page.

Set-PSReadlineKeyHandler -Key "Alt+%"  `
-BriefDescription ExpandAliases `
-LongDescription "Replace all aliases with the full command" `
-ScriptBlock {
param($key, $arg)

$ast = $null
$tokens = $null
$errors = $null
$cursor = $null
[PSConsoleUtilities.PSConsoleReadLine]::GetBufferState([ref]$ast, [ref]$tokens, [ref]$errors, [ref]$cursor)

$startAdjustment = 0
foreach ($token in $tokens)
if ($token.TokenFlags -band [System.Management.Automation.Language.TokenFlags]::CommandName)
$alias = $ExecutionContext.InvokeCommand.GetCommand($token.Extent.Text, 'Alias')
if ($alias -ne $null)
$resolvedCommand = $alias.ResolvedCommandName
if ($resolvedCommand -ne $null)
$extent = $token.Extent
$length = $extent.EndOffset - $extent.StartOffset
$extent.StartOffset + $startAdjustment,

# Our copy of the tokens won't have been updated, so we need to
# adjust by the difference in length
$startAdjustment += ($resolvedCommand.Length - $length)

Now let's see a before and after with this in use by using ALT+SHIFT+%.

Figure 5. Before using the keyhandler.


[Click on image for larger view.]  Figure 6. After using the key handler to expand the aliases.

Want a quick way to update your console PowerShell profile, just add this:

Set-PSReadlineKeyHandler -Key Ctrl+Alt+P  -BriefDescription "Edit  PowerShell profile in ISE" -ScriptBlock  {
ISE $Profile

If you want to see all of the available key handlers that are in PSReadline, then run the following command:


Before I leave you, I wanted to show off one more cool thing that you can when working in the console using PSReadLine and that is being able to use Ctrl+Space to see parameters in a command, possible parameter arguments (if applicable) and also static members of a type. Just use another Ctrl+Space after you have selected an item to use it in the command.

[Click on image for larger view.]  Figure 7. View possible parameters of a command.


[Click on image for larger view.]  Figure 8. View possible parameter values, if applicable.


[Click on image for larger view.]  Figure 9. View available static methods of a type.

This is just a few of the amazing things that are available with PSReadline. Other available things that I didn't cover here are:

  • Customizable key bindings
  • Cmd and emacs modes (neither are fully implemented yet, but both are usable)
  • Many configuration options
  • Bash style completion (optional in Cmd mode, default in Emacs mode)
  • Bash/zsh style interactive history search (CTRL-R)
  • Emacs yank/kill ring
  • PowerShell token based "word" movement and kill
  • Undo/redo
  • Automatic saving of history, including sharing history across live sessions

As you can see, there are so many things that you can do here to customize your experience and make it your own! So what are you waiting for? Give this a download and start experiencing a better way of working in the PowerShell console! Feel free to post your favorite key handlers that you use in the comments below.

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus
Most   Popular