- Interact with Azure Cosmos DB with PowerShell - Tue, Sep 14 2021
- Azure health services: Track Microsoft cloud outages and maintenance - Wed, Sep 8 2021
- Powerline: Customize your PowerShell console - Tue, Aug 31 2021
I'll never forget attending a technical conference several years ago, and seeing the presenter's command shell look like this on the overhead screen:
"What in the world is THAT?!" I wondered. "If only I could do something like that in PowerShell…"
I have good news for you, gentle reader: Today, you'll learn precisely how to configure your Windows command shell to display. Read on!
What is Powerline? ^
Powerline is a status line plug-in for the Linux VI iMproved (Vim) text editor. Specifically, Powerline for Linux is a Python application that enables you to format your command prompt with a combination of colorized text and symbols ("glyphs") to make it easier for you to see at a glance:
- Current user
- Computer host name
- Present working
- Git repository status
- Session time, system time, etc.
- Any other metadata you're interested in viewing
The good news for us is that kind fellow community members have ported Powerline's core functionality to Windows.
Note that you'll need to think ahead and install Powerline-compatible fonts so that your customized command prompt renders correctly. Otherwise, your command prompt will be filled with "sdf" Unicode placeholder characters—not a good look!
We'll get back to the font issue momentarily. First, let's set up Powerline for our PowerShell environment in Windows. This is what my PowerShell console looks like prior to Powerline:
Set up Powerline ^
I assume Git integration is a big reason why you want Powerline. Thus, as a reminder, make sure you've installed the Git client on your Windows workstation.
I also assume you're running PowerShell 7; if not, why not?! Anyhow, you'll need the PSReadline module, so get that installed as well:
Install-Module -Name PSReadLine -Scope CurrentUser -Force -SkipPublisherCheck
Install-Module -Name posh-git -Scope CurrentUser Install-Module -Name oh-my-posh -Scope CurrentUser
The posh-git module gives us tab completion for Git commands as well as Git repository status information. The oh-my-posh module supports PowerShell prompt theming.
Now, about fonts. As I said earlier, most fonts don't contain the glyphs Powerline requires to display stuff like the GitHub icon and so forth. Microsoft includes the Cascadia Code PL (with "PL" standing for "Powerline") in its free font pack, so this font might be a good starting point for you.
In the community, you'll also find several community members who release "hacked" font versions that include Powerline glyphs. For example, I'm in love with Caskaydia Cove Nerd Font, which I found at the Nerd Fonts archive. Go there, have fun trying out some patched fonts, and install any you love so that they're available to Powerline!
In the next image, you can see me using good ol' Windows Explorer to install a patched Powerline font. Extract the fonts, right-click the ones in question, and then select either Install or Install for all users from the shortcut menu. Done and done.
Configure the environment ^
Open your PowerShell profile (say, in Visual Studio Code):
Add the following lines to ensure Powerline is activated every time you launch PowerShell:
$env:POSH_GIT_ENABLED=$true Import-Module posh-git Import-Module oh-my-posh Set-PoshPrompt -Theme paradox
The first line, in which we add an environment variable to our PowerShell session, is required to enable posh-git to perform Git command autocompletion. The second and third lines ensure that the required modules are loaded into the runspace. The final line sets the desired Powerline theme with oh-my-posh's Set-PoshPrompt function.
"What other themes are available?" you ask. I have you covered. Run Get-PoshThemes and spend some time poring over the colorized Powerline goodness. Then edit the Set-PoshPrompt statement to your desired theme.
You'll notice all the Unicode substitution characters in the previous screenshot. Looks bad, doesn't it? Well, we need to configure our client application to use a Powerline-compatible font.
Here are some quick instructions for getting to the font controls of some favorite Windows PowerShell console applications:
- Open the control menu in the upper left corner of the window and select Properties.
- Navigate to the Font tab, make changes, and then click OK to commit.
- Open the Settings menu and browse to the appropriate profile.
- Navigate to the Appearance tab.
- Open the Font face list, make your change, and then click Save to commit.
Visual Studio code
- Click View > Command Palette, type settings, and choose Open Settings (JSON).
- Set the terminal.integrated.fontFamily and terminal.integrated.fontSize properties to the required values.
Check out the following screenshot, which shows my Get-PoshThemes output after I've completed my configuration:
If you want to format your terminal to show file and folder icons, perform the following actions to incorporate the Terminal-Icons module into your Powerline setup:
# Install the Terminal-Icons module Install-Module -Name Terminal-Icons -Repository PSGallery # Add the module import to your PowerShell profile Import-Module -Name Terminal-Icons
I'll finish by showing you what my finished console looks like. Dramatically different, eh?
Subscribe to 4sysops newsletter!
Wrapping up ^
Now that I'm using Powerline, I can't imagine not using it whenever I'm at a PowerShell console prompt. You're not limited to PowerShell either. For instance, learn how to enable Powerline in Windows Subsystem for Linux version 2 (WSL2) and the macOS bash prompt.