EDIT: These instructions are using Oh-My-Posh V2. An updated version of this post using Oh-My-Posh v3 is available.

 

When I teach a Kubernetes class at Kubernetes Academy, people always ask me how I configured my Powershell prompt to display the cluster name of my current context.  This is very useful if you're working with multiple clusters because it gives you constant feedback on the current K8s context.

Sorry but it's not a one-click install process so here are the steps:

1- Make sure that Git is already installed

2- Install Chocolatey, a package manager for Windows

3- Install the Windows Terminal from the Microsoft Store

4- Open your Windows Terminal as an admin, open a Powershell prompt and install a couple of Unix tools using Chocolatey.  We wil use them to parse the Kubernetes config file.

choco install grep
choco install sed

5- Install posh-git and oh-my-posh from the Powershell gallery

Install-Module posh-git -Scope CurrentUser
Install-Module oh-my-posh -Scope CurrentUser

6- If you use Powershell Core, install PSReadLine

Install-Module -Name PSReadLine -AllowPrerelease -Scope CurrentUser -Force -SkipPublisherCheck

7- Open your Powershell profile in Code (notepad will do if you don't have Code installed)

code $PROFILE

8- Import the Powershell modules, set a theme and while you're at it, why not create aliases for kubectl, docker and docker-compose? 😀

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox
Set-Alias -Name k kubectl
Set-Alias -Name d docker
Set-Alias -Name dc docker-compose

You can take a look at the available themes by heading to the Oh-My-Posh GitHub page

9- Install a font that supports ligatures like Cascadia Code PL or Fira Code

10- Locate the themes settings file by typing:

$ThemeSettings

11- Edit that file adding the following code that will retrieve the Kubernetes context and stuff it inside the prompt

$K8sContext=$(Get-Content ~/.kube/config | grep "current-context:" | sed "s/current-context: //")
    
If ($K8sContext) {
    $prompt += Write-Prompt -Object "$K8sContext" -ForegroundColor $sl.Colors.SessionInfoBackgroundColor -BackgroundColor $sl.Colors.GitDefaultColor
}

I placed that code here:

 

12- In the Windows Terminal, edit the settings.  This will launch your editor, in my case, Code.

13- Locate the Powershell section and set the fontFace to Fira Code or Cascadia Code PL.

{
    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "hidden": false,
    "name": "PowerShell",
    "source": "Windows.Terminal.PowershellCore",
    "cursorColor" : "#FFFFFF",
    "cursorShape" : "filledBox",
    "fontFace" : "Fira Code",
    "fontSize" : 12,
    "backgroundImage": "C:/Users/guyba/Pictures/Terminal/powershell-hero.png",
    "backgroundImageOpacity": 0.5,
    "backgroundImageStretchMode": "none",
    "backgroundImageAlignment": "bottomRight",
    "useAcrylic" : false
}

 

Bonus step! I'm also displaying an image in the bottom right section as a visual cue that reminds me that I'm in Powershell.

Here are the pictures I use 😀

Powershell

Command Prompt 

Azure Cloud Shell 

WSL 

 

Hope this helps!

 

Reference: Scott Hanselman blog post


Learn Docker and Kubernetes

Like this blog post? I also teach Docker and Kubernetes at Kubernetes Academy Online.

Kubernetes Academy Online is Offering Self-Paced Online Video Training Courses with complete Hands-on Exercises. Available for AWS, Azure, DigitalOcean, Google Cloud Platform, and Linode. In English or French. Instructor-led Virtual Classes also available.

Courses info: KubernetesAcademy.online

🔥 Use the following code at checkout to save $10 on my self-paced courses. 1 offer per account.

$10 OFF Discount Code: GUYB-BLOG