Latest posts by Michael Pietroforte (see all)
- Author and member of the year 2019 – Why DevOps still doesn't rule the IT world - Wed, Jan 1 2020
- Results of the 4sysops member and author competition in 2018 - Tue, Jan 8 2019
- Why Microsoft is using Windows customers as guinea pigs - Reply to Tim Warner - Tue, Dec 18 2018
Just to make your mouth water a little, these are PSReadLine’s main features: syntax coloring, syntax error notification, improved line editing, multiline editing, command completion, command history search, and keyboard shortcuts.
Multiline editing in PSReadLine
The installation instructions below might appear to be longwinded. However, the PowerShell beginner might fall into a few traps along the way. If you have imported third-party PowerShell modules before, you will know how to avoid these traps. You’ll also see that, thanks to the new package manager in PowerShell 5, things are getting a lot easier in Windows 10. However, you can also have comparable convenience with PowerShell 4 if you install a package manager on Windows 8.1, as you will see below.
Installing on Windows 8.1 with PowerShell 4 ^
First, you have to download PSReadLine.zip from GitHub.
Note: Don’t download PSReadLine-master.zip from the module’s main page. This file lacks the PSReadLine.dll file, and you will get this error message if you try to import the module:
Import-Module : The module to process 'PSReadLine.dll', listed in field 'NestedModules' of module manifest.
The next thing you should do is unblock the ZIP file. If you don’t unblock the file, you’ll get the following confusing error message when you try to import the module:
import-module : The specified module 'psdreadline' was not loaded because no valid module file was found in any module
To unblock the ZIP archive, right-click the file, select Properties, and then click Unblock.
Alternatively, you can run the following command on a PowerShell console:
I assumed here that you downloaded the file to the default download folder.
You can now unzip the file. If you don’t run PowerShell 5, I suggest that you do this with File Explorer because unzipping files in PowerShell 4 is quite cumbersome.
Because PSReadLine is a PowerShell module, you have to copy it to your PowerShell Modules folder in the Documents folder:
cp $env:USERPROFILE\Downloads\PSREadline\* $env:USERPROFILE\Documents\WindowsPowerShell\Modules\PSReadLine\
If you didn’t create the Modules folder before, this command will do it now.
Importing the PSReadLine module ^
To import the PSReadLine module, you have to launch a PowerShell console with administrator privileges and set your PowerShell execution policy to at least RemoteSigned:
Of course, Unrestricted and Bypass work, too.
To import the module, you need the following command:
If you don’t see any error message, PSReadLine should work now. To test it, you can run the Get-PSReadlineKeyHandler cmdlet to display the module’s key bindings.
If you like the module, you will want to import it automatically whenever you launch a PowerShell console. PowerShell’s module auto-loading won’t help you here because you usually won’t run one of the module’s cmdlets when you start working with the module. You should therefore import the module into your PowerShell profile.
I recommend adding the above import command to Microsoft.PowerShell_profile.ps1 in the WindowsPowerShell folder in your Documents directory (%USERPROFILE%\Documents\WindowsPowerShell) and not to profile.ps1. With the latter, you would also load the module when you start PowerShell ISE, which makes no sense because most of PSReadLine’s features won’t work there.
If you prefer to work with just one PowerShell profile, you can add these lines to profile.ps1:
if ($host.Name -eq 'ConsoleHost')
The if clause ensures that the module will only be imported when you start the PowerShell console.
Installing with PsGet from GitHub ^
The installation procedure in Windows 8.1 is a bit longwinded if you download the module in a browser. Things get easier if we do it the Linux way and download the module directly from the online repository GitHub.
As with the first installation method described above, you should set your PowerShell execution policy to at least RemoteSigned.
To access the GitHub repository from a PowerShell console, we first need to download and install the PsGet module:
(New-Object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1") | Invoke-Expression
The New-Object cmdlet creates the .NET object Net.WebClient and the object’s DownloadString method downloads the PowerShell script GetPsGet.ps1. We can execute the script that installs PSGet by piping it to the Invoke-Expression cmdlet.
With the help of the PSGet module we now download PSReadLine from GitHub:
Installing PSReadLine from GitHub
You can now import the module as described above.
Installing on Windows 10 with PowerShell 5 ^
In Windows 10, we don’t have to install the PsGet module because PowerShell 5 comes with PowerShellGet, a module that extends OneGet, the new package manager in PowerShell 5. Instead, from GitHub, you can install PSReadLine from the online repository PowerShell Gallery. As mentioned above, your PowerShell execution policy should be set to at least RemoteSigned.
You can simply download and install the PSReadLine module with this command:
Install-Module -Name PSReadLine
Installing PSReadLine on Windows 10
If you didn’t install a package from a NuGet-based gallery before, you will be asked to install NuGet-anycpu.exe. After that, the PSReadLine module will be downloaded. I recommend that you read the text, which warns you that you are installing a module from an untrusted repository.
You are now ready to import the PSReadLine module.
In my next post, I will show you how PSReadLine improves your PowerShell console experience.