Scott Hanselman

Towards a better console - PSReadLine for PowerShell command line editing

September 25, 2014 Comment on this post [19] Posted in Open Source | PowerShell
Sponsored By

Sometimes textmode is where it's at. I've long blogged about tools and techniques that will make your Windows console experience better. Perhaps you're a *nix person who is using Windows in your day job, or you wish the Windows PowerShell prompt was more nix-y. Or perhaps you're a PowerShell person who wants to take your command-line to the next level.

Well, just as NuGet is how we get .NET libraries quickly, and Chocolately is a kind of apt-get for Windows, PsGet is a way to easily add PowerShell modules to your prompt.

To install PsGet you run this script (feel free to vet it):

(new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | iex

Once you've got PsGet, the purpose of this post is to introduce you to PSReadLine.

To install PsReadLine with PsGet, just

install-module PsReadLine

If you want to install PsReadLine manually, you can from their readme. It does a LOT:

PsReadLine replaces the command line editing experience in PowerShell.exe. It provides:

  • Syntax coloring
  • Simple syntax error notification
  • A good multi-line experience (both editing and history)
  • 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
  • "Menu" completion (somewhat like Intellisense, select completion with arrows) via Ctrl+Space

But it doesn't replace it in a scary "moved my cheese" way, but in a comfortable familiar way, similar to how Bash works now. It will add things that you WILL miss when you move to another machine that doesn't have PsReadline. If you are already comfortable (or learning) PowerShell, this will feel comfortable immediately. It's not Dvorak. ;)

Some cool PsReadLine examples

Syntax coloring for things like keywords (cd) and common commands (git):

Syntax coloring with PSReadline

PowerShell often has you opening parentheses, brackets and things, and then you have to count them to close them. PsReadLine helps with that also:

Not only does it give you nice syntax-highlighting for things like function building, it also shows me with the red > that I haven't closed the block.

Forgot to close the block

When you are editing a multi-line script, you can also now backup to other lines!

multiline editing with PSReadline

If you are typing something like Get-Process and either want to autocomplete switches, or autocomplete results, you can press Ctrl-Space:

autocomplete

If you're advanced, check out get-PSReadlineKeyHandler and not only look at what functions are bound to which hotkeys, BUT also check out all the functions that AREN'T bound. You have a lot of power for customization here!

get-PSReadlineKeyHandler

You can even set Emacs keybindings!

Set-PSReadlineOption -EditMode Emacs

Go check out https://github.com/lzybkr/PSReadLine on GitHub and give it a star!

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
September 25, 2014 8:00
A couple of my favorite things about PSReadline you didn't mention (using the default "Windows" edit mode):

  • Ctrl+v pastes clipboard text to the console.

  • Ctrl+a, Ctrl+c copies the entire command line to the clipboard

  • Ctrl+z and Ctrl+y act as undo/redo for command line edits. Accidentally delete too many characters? Press Ctrl+z a few times.

  • PageUp and PageDown work as you would expect.

  • Ctrl+l clears the screen

  • You can get PSReadline to save history of all sessions and then search it later

September 25, 2014 9:20
Great point, Keith! The support for Ctrl-A,C,Z,V is reason enough to take the PsReadline plunge!
September 25, 2014 9:29
What about azure powershell? Do we have to install separately for azure powershell?
September 25, 2014 12:00
Sridhar - "Azure Powershell" is just a regular PowerShell console with some Azure-specific commands added. You can use them anywhere. See http://regularitguy.com/2014/03/13/installing-the-windows-azure-powershell-cmdlets/
September 25, 2014 12:05
@sridhar Yes, Azure PowerShell is just a regular PowerShell that imports Azure module on start. You can follow installation instruction from azure ps session, or just add `Import-Module Azure` to your $profile file.
September 25, 2014 12:06
It seems the module names are case-sensitive in the install-module call:
install-module PsReadline
(as in the post) doesn't work, but
install-module PsReadLine
does. (Or I just got some transient error when trying the first command.)
September 25, 2014 13:02
One of the most mind-boggling things about windows is that it STILL doesn't come with a decent console UI or text editor.
September 25, 2014 13:31
Copy and paste and keyboard shortcuts should not be functions in PowerShell modules. Cmd.exe should be fixed or replaced.
September 25, 2014 14:36
@Peter: I'd say cmd.exe is unlikely to be "fixed", but Console2 is a great replacement!
September 25, 2014 15:15
Yea, PSReadLine is really awesome. I also use ConsoleZ (https://github.com/cbucher/console) as a console host replacement.

These tools makes me not want to use powershell on a regular machine in the wild :)
September 25, 2014 17:35
Hmm, after installing, I can't use autocomplete anymore. Everytime I hit tab I get the following exception.

Exception:
System.MethodAccessException: Attempt by method 'PSConsoleUtilities.PSConsoleReadLine.DoReplacementForCompletion(System.Management.Automation.CompletionResult, System.Management.Automation.CommandCompletion)' to access method 'System.Management.Automation.CommandCompletion.set_ReplacementLength(Int32)' failed.
at PSConsoleUtilities.PSConsoleReadLine.DoReplacementForCompletion(CompletionResult completionResult, CommandCompletion completions)

at PSConsoleUtilities.PSConsoleReadLine.Complete(Boolean forward)
at PSConsoleUtilities.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
at PSConsoleUtilities.PSConsoleReadLine.InputLoop()
at PSConsoleUtilities.PSConsoleReadLine.ReadLine(Runspace remoteRunspace)
September 25, 2014 21:00
It's a great module. Thanks!
It would be very good to have something like intellisense for command parameters!
September 26, 2014 0:49
Not directly relevant, but I'd like to call out Cmder as a possible alternative to Console2. It's a shell over ConEmu, but is pretty off the shelf.
zc
September 26, 2014 1:42
Cool! I have this on my Apple II+ - it was called Global Program Line Editor! It'll be good to have similar functionality for PowerShell.

Greg
September 26, 2014 15:06
I get the following error...

Import-Module : The 'C:\...\WindowsPowerShell\Modules\PSReadLine\PSReadLine.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members t
hat are not valid ('RootModule'), then try to import the module again.
At C:\Users\Ali\Documents\WindowsPowerShell\Modules\PsGet\PsGet.psm1:1312 char:22
+ Import-Module <<<< -Name $ModuleBase -Global -Force:$Force
+ CategoryInfo : InvalidData: (C:\Users\Ali\Do...PSReadLine.psd1:String) [Import-Module], InvalidOperatio
nException
+ FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.ImportModuleCommand
September 26, 2014 16:00
Up graded ps to version 4 form 2 that worked.
Axe
September 28, 2014 0:08
PSReadLine is great! But after installing it another molude, Posh-Git, stopped working properly. Particularly, auto-complete of git commands and branch names stopped working for me.
September 28, 2014 10:24
Had the same issue than Tomij installing PsReadLine. PsGet help says that its commands are different than the ones in PowershellGet (WMF5) which I had already installed. Both modules provide an install-module command.

What worked for me was disabling PowershellGet temporary while installing PsReadLine as follows:
remove-module PowershellGet
import-module PsGet
install-module PsReadLine

HTH.
September 30, 2014 23:12
Great addon, thankfully it didnot break anything else for me.
My only gripe with this is that the highlighting (coloring) doesn't work as well on PowerShell ISE as it does on PowerShell.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.