DEV Community

Cover image for Improve your PowerShell experience
Daniel Marczin
Daniel Marczin

Posted on • Originally published at marczin.dev

Improve your PowerShell experience

Completions

Command completion is somewhat lackluster by default in PowerShell.
Most of the built-in functions have tab-completion support, but for those of us
socialized on better different shells, it might be a bit unusual at first.
Out of the box, TAB invokes the tab completion for files and cmdlets with subsequent presses cycling through the available suggestions.
Pressing Ctrl+Space provides a selection, similar to what ZSH offers for TAB presses.

You can remap these, if you don't want to bother re-learning them.
First, let's open your profile.

code $PROFILE # just use your favourite editor
Enter fullscreen mode Exit fullscreen mode

Then add the following line(s), setting the keys to whatever you're accustomed to:

Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
Set-PSReadlineKeyHandler -Key Shift+Tab -Function TabCompleteNext
Enter fullscreen mode Exit fullscreen mode

To list all the shortcuts with descriptions, just use the command
Get-PSReadLineKeyHandler

Go the extra mile

You can also install the latest version of PSReadLine, which is responsible for your completions, by running:

Install-Module -Name PSReadLine
Enter fullscreen mode Exit fullscreen mode

The latest versions utilize the command history and various plugins for better completions.
One of these plugins is CompletionPredictor, which can be installed just like we did with PSReadLine,
and provides IntelliSense-like suggestion. You can toggle between the default and IntelliSense recommendations by pressing F2.

Install-Module -Name CompletionPredictor
Enter fullscreen mode Exit fullscreen mode

In PWSH you can press F1 after entering a command to see its man-page.

Just use Bash? 👀

While I personally didn't have much luck with this tool,
if you really need those completions for that one program that you use all the time (provided it has completions defined for bash),
you might opt to try your luck with PS bash completions.
This module acts as a bridge between bash and PowerShell, providing you with completions (with some limitations).

Make things pretty

Install Oh My Posh (á la Oh My ZSH), available on Windows, Linux and macOS. Just follow the instructions in their docs.

# Windows
winget install JanDeDobbeleer.OhMyPosh -s winget

# macOS & Linux
brew install jandedobbeleer/oh-my-posh/oh-my-posh
Enter fullscreen mode Exit fullscreen mode

Then install a NerdFont, to display some fancy glyphs, by running:

sudo oh-my-posh font install
# or to run it without elevation use: --user
Enter fullscreen mode Exit fullscreen mode

..and don't forget to configure your terminal to actually use the font(s) that you've just installed.

Next, configure a theme that you like. You can just run Get-PoshThemes which displays all the available themes, and even tells you how set one permanently.

Use git shorthands

You can use the same git aliases that you do in Oh My Zsh by installing the module powershell-git-aliases.
If you are not familiar with them, Oh My ZSH provides a full list on their GitHub.

Just run the usual command to install it and add the module to your profile.

Install-Module git-aliases -Scope CurrentUser -AllowClobber

# then add this to your $PROFILE, and restart your terminal:

Import-Module git-aliases -DisableNameChecking
Enter fullscreen mode Exit fullscreen mode

There it is! Now you can use your gcos, gpras and ggfls to your heart's content.

Top comments (0)