Using PSReadLine in PowerShell – CloudSavvy IT

Powershell logo

PSReadLine is one of those modules that may not immediately show its utility until regular use. If you use the command-line of PowerShell often, PSReadLine can make your life much easier. Included in PowerShell versions as far back as Windows PowerShell 5, PSReadLine continues to add new features and utility. Building on the venerable legacy of GNU Readline in the Unix world, PSReadLine adds additional features to the PowerShell command-line experience such as multi-line editing, syntax coloring, predictive IntelliSense, richer history support, and alternative edit modes.

Updating and Loading PSReadLine

Most relatively recent versions of PowerShell have a bundled version of PSReadLine. Included as far back as Windows PowerShell 5.0, each subsequent version has added a newer version with PowerShell 7.1 included PSReadLine 2.1. Of course, you may want to take advantage of newer features in older PowerShell versions. In case PSReadLine is not already imported, use Import-Module PSReadLine to start using the features right away. To make sure you are using the latest version, read on!

Beginning with PowerShell 7.0, PowerShell skips auto-loading PSReadLine on Windows if a screen reader program is detected. Currently, PSReadLine doesn’t work well with screen readers. The default rendering and formatting of PowerShell 7.0 on Windows works properly. You can manually load the module if necessary.

Windows PowerShell 5.x

There are generally two steps to updating PSReadLine with Windows PowerShell 5.0 or 5.1. First, you need to make sure you are running version 1.6.0 or higher of PowerShellGet. To do this, you need to run the following command in an elevated Windows PowerShell session.

Install-Module -Name PowerShellGet -Force

Next, make sure that all PowerShell sessions are closed and in an elevated cmd.exe prompt run the following code. The reason that this is run from cmd.exe is that, by default, PSReadLine is loaded and can’t be updated if in memory.

powershell -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease"

PowerShell 6.x Core and PowerShell 7

To update PSReadLine in the newer versions of PowerShell, you can do a similar operation by closing all open PowerShell sessions, pwsh.exe, and run an elevated cmd.exe session with the following code.

pwsh.exe -noprofile -command "Install-Module PSReadLine -Force -SkipPublisherCheck -AllowPrerelease"

Updating PowerShell Gallery Installed PSReadLine

You may have installed PSReadLine from the PowerShell Gallery, and if you have done this, you can run either of the following commands to update depending on whether you are on Windows PowerShell or PowerShell Core/7.

# Windows PowerShell
powershell -noprofile -command "Update-Module PSReadLine -AllowPrerelease"

# PowerShell Core/7
pwsh.exe -noprofile -command "Update-Module PSReadLine -AllowPrerelease"

Features of PSReadLine

What can we do with PSReadLine? There are a number of very cool features that can help you with your command-line experience. Below you will see how several features work in practice.

Syntax Coloring

An example of how syntax coloring makes the readability of the command-line much easier, see this example where you can see that the output is colored, for the variable names, function names, and comparison operators.

Although this command line isn’t terribly long, there are cases where splitting this across multiple lines would be much easier to deal with. Read on, to see how we can split this over multiple lines, just like a traditional code editor.

Multi-Line Editing

Taking the same command series above, how do we split this across multiple lines? Using the default key combination of Shift-Enter (on Windows), we can split across lines as denoted by >>.

You can use arrow keys to navigate around the text and only when you are ready to run the command, you can hit the Enter key to execute. What about when you aren’t sure command to use? Predictive Intellisense to the rescue!

Predictive Intellisense

First, we need to enable Predictive IntelliSense. You can tell it to use different source types, but the most common is simply your history. Keep in mind that until you have a history file with content built up this may not be the most useful, but after a while it becomes handy.

# This will enable the prediction source for IntelliSense
Set-PSReadLineOption -PredictionSource 'History'
# (Optional) Change the darker grey to a lighter grey to account for dark backgrounds
Set-PSReadLineOption -Colors @{ InlinePrediction = '#9CA3AF'}

Now we can test this out using the same command that we just ran. You can see that after just typing Get we already see the command offered for completion. You can use the right arrow (default) to accept the selection.

PSReadLine History File

Since we have talked about the history file, it can be useful to know where this file is actually stored. What’s contained within is each command on a new line, with no additional data in a simple format.

  • Windows – $env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine
  • Linux – $env:XDG_DATA_HOME/powershell/PSReadLine or $env:HOME/.local/share/powershell/PSReadLine

Be aware! When using cmdlets like ConvertTo-SecureString passwords may be stored in the history file.

Conclusion

PSReadLine is a very useful module to enhance your command-line experience. With multi-line editing, syntax highlighting, and the recent addition of Predictive IntelliSense, it is an indispensable addition to any system administrators toolkit!

Source link

The post Using PSReadLine in PowerShell – CloudSavvy IT appeared first on TechFans.