I spent a lot less time in PowerCLI these days which means my profile hasn’t changed much and my prompt was still the same as when I customized it several years ago. Because I use Linux in WSL, I have to get up to speed with it and customizing your prompt and profile is just part of the journey.

I work mostly in Windows Terminal and so far I’m using Starship to customize WSL after being recommended by a colleague so I thought I’d try something else to refresh my PowerShell profile. I went forOh My Posh, a pretty similar shell customization tool and very easy to set up.

  • Install Windows Terminal if you don’t already have it. (It doesn’t have to be Windows Terminal).

  • Install a Nerd font to get icons support and set Windows Terminal to use it.

  • Install Oh My Posh in PowerShell.

Install-Module oh-my-posh -Scope CurrentUser
  • Import the module in your PowerShell profile ($PROFILE)
Import-Module oh-my-posh
  • Set it up to use a theme of your choosing, Agnoster for instance. Add this to your PowerShell profile ($PROFILE).
Set-PoshPrompt -Theme agnoster

vcenter with powershell oh my posh

The prompt already looks cool but I want it to display which vCenter I am connected to. Something I used a lot in my previous profile. There is currently no support for this so we have to make our own.

  • Export the current profile to a file.
Export-PoshTheme -FilePath "~/.mytheme.omp.json" -Format json
  • Edit the json file and use the TEXT feature to add a vCenter record next to the username. We will place the following fragment after the “sessions” section. This will check for an environment variable named ConnectedVCenter that we’ll manage in the powershell profile.
{
"background": "#cce9aa",
"foreground": "#100e23",
"powerline_symbol": "\ue0b0",
"properties": {
"template": " \uf6a6  "
},
"style": "powerline",
"type": "text"
},
  • Now edit your PowerShell profile ($PROFILE). Remove or comment out the “Set-PoshPrompt” command and add the following fragment. This will populate an env variable when a vCenter is connected.
#Set-PoshPrompt -Theme agnoster

oh-my-posh prompt init pwsh --config "~/.mytheme.omp.json" | Invoke-Expression

function Set-EnvVar {
$env:ConnectedVCenter = ($global:DefaultVIServers | where isconnected -eq $true).name
}
New-Alias -Name 'Set-PoshContext' -Value 'Set-EnvVar' -Scope Global -Force

Now the PowerShell prompt should show the connected vCenter(s). Pretty cool right?

vcenter with powershell oh my posh

Anyway, I played a bit with it so I thought I’d write a super short piece about it. A longer version of this article with a lot more details will come out on the Altaro blog at some point so watch this space.