PowerShell modules for a better command line

November 19th 2021 PowerShell

Inspired by Scott Hanselman's blog post and a discussion with my colleagues, I further improved my PowerShell configuration by installing a few select PowerShell modules.

Terminal-Icons

Terminal-Icons does exactly what its name implies: it adds icons to the file lists in the terminal.

File listing with Terminal-Icons

To add the module to your PowerShell configuration, first install it from your PowerShell prompt:

Install-Module -Name Terminal-Icons -Repository PSGallery

Then add the following to your $PROFILE file:

Import-Module -Name Terminal-Icons

PSReadLine

PSReadLine adds a lot of functionality. Out of those, I use the most:

  • Ctrl+Space for IntelliSense-like completion. To see it in action, type part of the file and press the key combination.

    PSReadLine completion

  • Ctrl+R to activate interactive history search for a substring. You can then use Ctrl+R and Ctrl+S to move backward and forward through the results. Enter to use the selected result.

    PSReadLine history search

  • Up Arrow and Down Arrow to scroll through the history entries with the current prefix. This is not enabled by default and requires an additional entry in $PROFILE (see below):

    Move through history by prefix

To use it in your configuration, install it from the gallery:

Install-Module PSReadLine -AllowPrerelease -Force

And add to the $PROFILE file:

if ($host.Name -eq 'ConsoleHost')
{
    Import-Module PSReadLine

    # Binding for moving through history by prefix
    Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
}

PSFzf

My final PowerShell module of choice is PSfzf. It integrates the well-known fuzzy finder, fzf, into PowerShell:

  • Ctrl+T searches the paths below the current directory and inserts the selected path: Fuzzy search paths
  • Alt+C does the same, but automatically changes the current directory to the selected one upon confirmation.
  • Ctrl+R does a fuzzy search of the history entries. It requires an additional entry in $PROFILE to override the default functionality of PSReadLine. Fuzzy history search
  • Tab enables fuzzy search for tab completion. If you confirm the selection with \, completion continues for the next part of the path, Enter confirms the path. The functionality requires an entry in $PROFILE to override the default behavior of tab completion. Fuzzy tab completion

Besides the default fuzzy search, you can also use substring search (start query with '), prefix search (start query with ^), suffix search (end query with $), and more.

To use PSFzf, you must first install fzf. I prefer to use Chocolatey (you must install it first for the following command to work):

choco install fzf

Then install the PowerShell module from the gallery:

Install-Module PSFzf

And finally configure it in the $PROFILE file:

Import-Module PSFzf

# Override PSReadLine's history search
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' `
                -PSReadlineChordReverseHistory 'Ctrl+r'

# Override default tab completion
Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }

I have gotten so used to these PowerShell modules that I miss them as soon as I use a command prompt without them. I am pretty happy with my current configuration. But who knows, maybe I'll find more modules to add in the future. What does your PowerShell configuration look like?

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License