قالب وردپرس درنا توس
Home / Tips and Tricks / Using PSReadLine in PowerShell – CloudSavvy IT

Using PSReadLine in PowerShell – CloudSavvy IT



Powershell logo

PSReadLine is one of those modules that will only immediately show its usefulness with regular use. If you use the PowerShell command line often, PSReadLine can make your life much easier. PSReadLine has been included in PowerShell versions as far back as Windows PowerShell 5 and continues to add new features and tools. Building on GNU Readline̵

7;s venerable legacy 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 alternate editing modes.

Update and load PSReadLine

Most relatively recent versions of PowerShell have a bundled version of PSReadLine. Included already in Windows PowerShell 5.0, each subsequent version has added a newer version with PowerShell 7.1 including PSReadLine 2.1. Of course, you may want to take advantage of newer features in older PowerShell versions. If PSReadLine has not been imported yet, use Import-Module PSReadLine to use the functions immediately. Read on to make sure you’re using the latest version!

Starting with PowerShell 7.0, PowerShell skips the automatic loading of PSReadLine on Windows if a screen reader program is detected. Currently PSReadLine does not work well with screen readers. The default view and formatting of PowerShell 7.0 on Windows works correctly. You can manually load the module if necessary.

Windows PowerShell 5.x

There are generally two steps to update PSReadLine with Windows PowerShell 5.0 or 5.1. First, make sure you’re using the version 1.6.0 or higher from 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 all PowerShell sessions are closed and in an elevated cmd.exe prompt run the following code. The reason why this is being done cmd.exe is that PSReadLine is loaded by default and cannot be updated if it is 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 perform 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 once you did, you can run one of the following commands to update depending on whether you are using 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 some really cool features that can help with your command line experience. Below you can see how various features work in practice.

Syntax colors

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

While this command line isn’t very long, there are cases where splitting it across multiple lines would be much easier. Read on to learn how we can split this across multiple lines, just like a traditional code editor.

Editing multiple rules

Using the same command string above, how can we divide this across multiple lines? With the standard Shift-Enter key combination (in Windows) we can split across lines as indicated by >>.

You can use the arrow keys to navigate through the text, and only when you are ready to run the command can you press Enter to run. What if you are not sure whether to use the command? Predictive Intellisense to the rescue!

Predictive Intellisense

First, we need to enable Predictive IntelliSense. You can tell to use different resource types, but the most common is just your history. Keep in mind that until you have a history file of accumulated content, it may not be the most useful, but it will become useful after a while.

# 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 with the same command we just ran. You can see that after typing Get we already see the command presented for completion. You can use the right arrow (default) to accept the selection.

PSReadLine history file

Since we’ve talked about the history file, it can be helpful to know where this file is actually stored. What’s inside is every 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

Pay attention! When using cmdlets such as ConvertTo-SecureString passwords can be stored in the history file.

Conclusion

PSReadLine is a very useful module to improve 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 administrator toolkit!


Source link