PowerShell Customization

Finally I was able to take some time to “pimp” my powershell prompt, I had took the time to customize my zsh prompt both in my mac and pc (using WSL) in order to get Powerline9k.

Anyways, I followed this guide which I stumbled upon on Twitter; the poster showed it running on Windows Terminal, I have to settle on my current config of ConEmu given that I have not installed Windows May Update (1903)

The guide is pretty straight forward, some variations I had to make:

if (Test-Path ~/.azure/clouds.config) {
  #$currentSub = & sed -nr "/^\[AzureCloud\]/ { :l /^subscription[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" ~/.azure/clouds.config
  $subscriptionLine = ((Get-Content -Path ~/.azure/clouds.config) -Match "^subscription(\ =)")[0]
  if ($null -ne $subscriptionLine) {
    $currentSub = $subscriptionLine.Substring($subscriptionLine.Length - 36)
    $currentAccount = (Get-Content ~/.azure/azureProfile.json | ConvertFrom-Json).subscriptions | Where-Object { $_.id -eq $currentSub }
    if ($null -ne $currentAccount) {
      Write-Host " " -NoNewLine
      Write-Host " $([char]0xf0c2)$([char]0xfd03)" -NoNewLine -BackgroundColor DarkCyan -ForegroundColor Yellow
      Write-Host " $($currentAccount.name) " -NoNewLine -BackgroundColor DarkCyan -ForegroundColor White
    }
  }
}

The substitution is the commented line

#currentSub = ...

with

$subscriptionLine = ((Get-Content -Path ~/.azure/clouds.config) -Match "^subscription(\ =)")[0]

$subscriptionLine ends up being an array of matching lines, which in our case is just one, hence accessing it by index 0, thence it is further parsed to extract the Subscription GUID.

The rest of the code is already checked in to my PowerShell Profile Repository.

References