Exploring PSReadLine
Early versions of PowerShell were monochrome, although the terminal (conhost.exe
) did provide some limited coloring. These versions of Windows PowerShell also lacked some of the cool features found in Linux shells.
With PowerShell 4, PowerShell included a new module, PSReadLine
. The module provides a command-line editing experience that is on a par with the best of the Linux command shells (e.g., BASH). The PSReadLine module provides additional console editing features within both PowerShell 7 and Windows PowerShell.
When you type into a PowerShell console, PSReadLine
intercepts your keystrokes to provide syntax coloring, simple syntax error notification, etc. PSReadLine
enables you to customize your environment to suit your personal preferences. Some key features of the module include:
- Syntax coloring of the command-line entries
- Multiline editing
- History management
- Customizable key bindings
- Highly customizable
For an overview of PSReadLine, see https://learn.microsoft.com/powershell/module/psreadline/about/about_psreadline. And for more details, you can view the PSReadLine’s GitHub README file: https://github.com/PowerShell/PSReadLine/blob/master/README.md.
There are several minor issues you may need to understand. One issue is the naming of this module. The original name of the module was PSReadline
. At some point, the module’s developers changed the module’s name to PSReadLine
(capitalizing the L character in the module name). Unfortunately, that change caused Update-Help
to fail since there is case sensitivity in module names). You can fix this by manually updating the module’s folder name from PSReadline
to PSReadLine
.
Another issue arises if you use VS Code. The PSReadLine
module ships natively with PowerShell 7. If you use VS Code’s PowerShell Integrated Terminal, you cannot load any newer version of PSReadline
. At least until the development team updates the PowerShell extension to utilize the updated version of PSReadLine. This is by design. For most IT pros, this probably does not matter much. But you may find later versions of PSReadLine contains features you want – if so, you should be able to use the Preview (i.e., beta!) version of the PowerShell extension, which supports the latest version of PSReadLine.
A final issue relates to changes made at V2. With the module’s V2 release, the dev team made some changes that were not backward compatible. But be aware that some older scripts may need adjusting. Many blog articles, for example, use the older V1 syntax for Set-PSReadLineOption
, which fails with later versions of the module. You may still see the old syntax if you use your search engine to discover examples. Likewise, some of the examples in this recipe fail should you run them utilizing PSReadline V1. Over time, though, the documentation and blog posts should catch up.
You run this recipe on SRV1
after you have installed PowerShell 7. Run this recipe in VS Code after configuring VS Code and loading the Cascadia Code font.
How to do it...
- Getting commands in the PSReadline module
Get-Command -Module PSReadLine
- Getting the first 10 PSReadLine key handlers
Get-PSReadLineKeyHandler | Select-Object -First 10 Sort-Object -Property Key | Format-Table -Property Key, Function, Description
- Discovering a count of unbound key handlers
$Unbound = (Get-PSReadLineKeyHandler -Unbound).count "$Unbound unbound key handlers"
- Getting the PSReadline options
Get-PSReadLineOption
- Determining the VS Code theme name
$Path = $Env:APPDATA $CP = '\Code\User\Settings.json' $JsonConfig = Join-Path $Path -ChildPath $CP $ConfigJSON = Get-Content $JsonConfig $Theme = $ConfigJson | ConvertFrom-Json | Select-Object -ExpandProperty 'workbench.colorTheme'
- Changing the VS Code colors
If ($Theme -eq 'Visual Studio Light') { Set-PSReadLineOption -Colors @{ Member = "'e[33m" Number = "'e[34m" Parameter = "'e[35m" Command = "'e[34m" } }
How it works...
In step 1, you use Get-Command
to discover commands in the PSReadLine module, with
Figure 1.35: Discovering commands in the PSReadLine module
In step 2, you use Get-PSReadLineKeyHandler
to discover some of the key handlers implemented by PSReadline, with output like this:
Figure 1.36: Viewing ten PSReadLine key handlers
In step 3, you calculate how many key handers are unbound and are available for you to use. The output from this step is:
Figure 1.37: Counting unbound key handlers
PSReadLine has many options you can set. In step 4, you use the Get-PSReadLineOption
command to view the option settings, with output like this:
Figure 1.38: Counting unbound key handlers
In step 5, you determine the current VS Code theme, and in step 6, you change the PowerShell token colors, but only if the theme set is the Visual Studio Light theme. These two steps produce no console output.
There’s more...
In step 1, you open a new Windows PowerShell console. Make sure you run the console as the local administrator.
In step 3, you view the first ten of the PSReadLine’s key handlers. Using PowerShell, PSReadLine captures specific keyboard sequences (e.g., Alt + L) and uses an assigned key handler to carry out some action. Typing Alt + L
clears the terminal window (in VS Code and the PowerShell console). PSReadline implements a range of key handers with plenty of room for you to customize the editing experience and provide significant customization of the shell. As you can see in step 3, you can use over 100 key combinations to implement your own customizations.
The screenshots throughout most of this book use this color theme. You could extend your profile files to update token colors each time you start a VS Code terminal.
Join our community on Discord
Join our community’s Discord space for discussions with the author and other readers: