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 multiples 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 promtpt 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 more about my Docker/Kubernetes virtual courses offered by Kubernetes Academy


Learn Docker and Kubernetes

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

Kubernetes Academy offers instructor led training workshop-style on Docker containers and Kubernetes for web developers, DevOps specialists, I.T. Pros. With our virtual classes, you get the same training experience and the same interaction with the trainer.

Courses info, dates and prices: LearnK8s.ca

20% OFF Discount Code: GUYB-BLOG