PowerShell is a powerful tool that can be used to manage many different tasks. One of the tasks that PowerShell can be used to manage is reading files. This article will show you how to use PSReadLine in PowerShell to read files. First, you need to install the PSReadLine module. To do this, you can either type the following command into a PowerShell prompt: $module = “PSReadLine” Or you can install it using the following command: Install-Module -Name PSReadLine
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!
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.
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 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.
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.
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.
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
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!