Scott Hanselman

Towards a better console - PSReadLine for PowerShell command line editing

September 25, '14 Comments [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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Thursday, 25 September 2014 04:00:43 UTC
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

Thursday, 25 September 2014 05:20:08 UTC
Great point, Keith! The support for Ctrl-A,C,Z,V is reason enough to take the PsReadline plunge!
Scott Hanselman
Thursday, 25 September 2014 05:29:04 UTC
What about azure powershell? Do we have to install separately for azure powershell?
Thursday, 25 September 2014 08:00:49 UTC
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/
Scott Hanselman
Thursday, 25 September 2014 08:05:39 UTC
@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.
Thursday, 25 September 2014 08:06:34 UTC
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.)
TomiJ
Thursday, 25 September 2014 09:02:41 UTC
One of the most mind-boggling things about windows is that it STILL doesn't come with a decent console UI or text editor.
Fergal Moran
Thursday, 25 September 2014 09:31:40 UTC
Copy and paste and keyboard shortcuts should not be functions in PowerShell modules. Cmd.exe should be fixed or replaced.
Thursday, 25 September 2014 10:36:32 UTC
@Peter: I'd say cmd.exe is unlikely to be "fixed", but Console2 is a great replacement!
Pedro
Thursday, 25 September 2014 11:15:19 UTC
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 :)
Tamas
Thursday, 25 September 2014 13:35:22 UTC
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)
Kris Harper
Thursday, 25 September 2014 17:00:57 UTC
It's a great module. Thanks!
It would be very good to have something like intellisense for command parameters!
AliS
Thursday, 25 September 2014 20:49:41 UTC
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
Thursday, 25 September 2014 21:42:56 UTC
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
Greg Sohl
Friday, 26 September 2014 11:06:02 UTC
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
Axe
Friday, 26 September 2014 12:00:08 UTC
Up graded ps to version 4 form 2 that worked.
Axe
Saturday, 27 September 2014 20:08:43 UTC
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.
Sunday, 28 September 2014 06:24:52 UTC
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.
Juan Carlos
Tuesday, 30 September 2014 19:12:34 UTC
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.