How to Display the Current Azure Subscription in your CLI

In 2019, I wrote an article about the tools you can use to improve your terminal experience when working with Azure; check it out here if you want to improve your Azure terminal experience. One of the tools I used to customise the command line is Oh-My-Posh (OMP), at the time, this was version 2, but now version 3 has been released, which adds a whole new way to add custom “segments” in your CLI. Segments allow you to determine what information is displayed in the command line prompt, and Oh-My-Posh has a whole range of segments that can include data from Git, Dotnet, Spotify and more.

The segment we’re going to look at today is the Azure one. This allows us to display the currently selected Azure subscription in the CLI so we can always see which sub we are in. Once installed, it’s going to look like this:

Install Oh-My-Posh

The first thing we need to do to get this working is to install OMP. You can install this in a variety of CLI tools now, but we will focus on doing this in PowerShell. Installing can be done simply using PowerShell get:

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

Setup a PowerLine Font

OMP displays various symbols in the CLI; several of these fonts are only available in font sets that support “PowerLine” fonts, so we need to install and use one of these. I like to use the “Cascadia Code” font, which was designed for use with Windows Terminal, and Microsoft does have a PowerLine version of this which you can get details of here. However, I have found that this does not contain all the symbols needed by OMP. Instead, NerdFont has a patched version of Cascadia called “Caskaydia”, which does have the correct characters. You can download this here. Extract the files, select them all and right-click to install.

Once installed, we need to get the terminal to use them. For the Windows Terminal, you can open the settings file, and then for each CLI, add or replace the FontFace value:

"fontFace": "CaskaydiaCove Nerd Font"

So it should look something like this:

{
    "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
    "hidden": false,
    "name": "PowerShell",
    "source": "Windows.Terminal.PowershellCore",
    "fontFace": "CaskaydiaCove Nerd Font"
},

Choose a Theme

Oh-My-Posh comes with various themes for how the CLI will look. You can see examples of all the themes by running the Get-PoshThemes command.

Themes

Once you have had a look through and picked the theme, you want you can select it using the Set-PoshTheme command, e.g.

Set-PoshPrompt aliens

This will set the theme for the current session but won’t persist beyond that. We will make this permanent shortly.

Export and Update Configuration

Currently, we are using the default configuration for OMP. To set up the Azure segment, we need to export this configuration to a file, so we have a starter theme that we can then edit to add our requirements and then use this as the new theme.

We can export the current setup using the Export-PoshTheme command. This exported file will become the theme you use, so you want to export it somewhere safe. If you plan on using OMP on multiple machines, you may want to put it in some sort of cloud storage like OneDrive or Dropbox so that you can use the same theme across various devices.

Export-PoshTheme -FilePath c:\ompthemes\mytheme.omp.json -Format json

Once you run this command, you should find the mytheme.omp.json file in the location you selected. You can now open this in your editor of choice.

You’ll see many settings here; you can play with any of them to tweak the console to how you want. However, the bit we are interested in is the “segments” section. You’ll find that there are multiple segments installed by default depending on which theme you use, for example:

  • Root - this segment determines what is displayed when you are running as root or in an elevated prompt

  • Session - this segment shows the current user session details, the blue section in the example below

  • Path - this segment determines how your current path is displayed, the pink section in the example below

  • Git - this segment determines how the git status of the current path is shown if you are in a git folder. The green section in the example below

Example Prompt

You can edit or remove any of these segments if you don’t like them.

To get our Azure details in the prompt, we need to add a new section using the Azure segment described here. We add the following into the segments array:

{
  "type": "az",
  "style": "powerline",
  "powerline_symbol": "\uE0B0",
  "foreground": "#000000",
  "background": "#9ec3f0",
  "properties": {
    "display_id": true,
    "display_name": true,
    "info_separator": " @ ",
    "prefix": " \uFD03 "
  }
}

Save the file, then to have the current prompt reflect your changes, you can run the Set-PoshTheme command again, pointing at your theme file:

 Set-PoshPrompt -Theme c:\ompthemes\mytheme.omp.json

This will reload the prompt, and you should now see a new segment showing the current Azure Subscription ID and name.

Azure Theme

This value will automatically update as you change subscriptions at the CLI. You can tweak the theme file to adjust how this looks, colours etc.

Load OMP Automatically

If you close and re-open the CLI, OMP won’t be loaded, so we need to update our PowerShell profile to load this automatically. Open your PowerShell profile in your editor of choice. To do this in VS Code, just run the command below:

code $profile

If you have an existing PowerShell profile, this will open it; if not it will create a new one in the right place. Add the following line to the file, replacing the path with your theme file’s actual path.

Set-PoshPrompt -Theme c:\ompthemes\mytheme.omp.json

Save this file. If you now close and re-open your terminal, it should load OMP with the right theme.

Bonus Config - Kubernetes

If you spend a lot of time working with Kubernetes in Azure, then there is another segment that may be useful, which will show the current Kubernetes cluster and namespace. To add this, just add another segment, this time with the Kubernetes config:

{
  "type": "kubectl",
  "style": "powerline",
  "powerline_symbol": "\uE0B0",
  "foreground": "#000000",
  "background": "#ebcc34",
  "properties": {
    "prefix": " \uFD31 ",
    "template": "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}"
  }
}

You can customise the template section to have it display whatever information you want from the Kubernetes context.

Lot’s More Segments

I’d encourage you to take a look at this list of segments in the docs to see if there are any others that might help make your CMD prompt even more helpful.