Friday, November 27, 2026

Azure CLI: Working with Multiple Tenants Side by Side using AZURE_CONFIG_DIR

In this post, let's have a look at how I work with multiple Azure tenants, signed in with different accounts, at the same time using the Azure CLI.

I use az a lot, and I often work across tenants that belong to different accounts. A single az sign-in can hold more than one account, but it has only one current subscription, and every terminal shares it. Run az account set in one terminal, and the command you run in another terminal a minute later goes to a different subscription, possibly in a different tenant. That's quite a pain.

I am using Azure CLI 2.93.0 (latest as of today, it will change).

What is AZURE_CONFIG_DIR

AZURE_CONFIG_DIR is an environment variable that tells the Azure CLI where to keep its state. When it's not set, it defaults to %USERPROFILE%\.azure on Windows ($HOME/.azure on Linux and macOS). Every time az runs, it reads and writes everything in that folder.

If you look inside, these are the files that matter:
  • azureProfile.json: the signed-in accounts, the subscriptions you can see, and which one is current (isDefault). This is what az account set changes.
  • msal_token_cache.bin (on Windows): the token cache. Your tokens live here, not in azureProfile.json.
  • config: whatever you set with az config (default resource group, location, output and so on).
  • cliextensions and bin: installed extensions and the Bicep CLI.
So the folder is basically your Azure CLI identity. Point AZURE_CONFIG_DIR at a different folder, and you get a completely separate sign-in, current subscription and token cache. And since environment variables are per process, each terminal can point at its own folder.

I have one folder per account.
C:\Users\Jaliya\.azure            # Personal (the default)
C:\Users\Jaliya\.azure-contoso    # Contoso tenant
C:\Users\Jaliya\.azure-fabrikam   # Fabrikam tenant
...
To set one up, point the variable at a new folder and sign in. The folder gets created the first time az runs.
$env:AZURE_CONFIG_DIR = "$HOME\.azure-contoso"

az login `
    --tenant contoso.onmicrosoft.com

az account show `
    --query user.name `
    --output tsv
Open another terminal without the variable, run az account show, and you are still signed in with your personal account. Neither terminal can affect the other.

Switching automatically by folder

Setting the variable by hand gets old fast, and forgetting to set it is exactly the mistake we are trying to avoid. All my code for a given client lives under one root folder, so I let my PowerShell profile pick the config folder based on the current location.
# One Azure CLI profile per folder root; the default ~/.azure everywhere else
$env:AZURE_EXTENSION_DIR = "$HOME\.azure\cliextensions"

$script:AzureConfigDirByRoot = [ordered]@{
    'D:\Repos\Contoso'  = "$HOME\.azure-contoso"
    'D:\Repos\Fabrikam' = "$HOME\.azure-fabrikam"
}

function Set-AzureConfigForLocation {
    $path = (Get-Location).ProviderPath
    foreach ($root in $script:AzureConfigDirByRoot.Keys) {
        if ($path -eq $root -or $path.StartsWith("$root\", 'OrdinalIgnoreCase')) {
            $script:AzureConfigDirAuto = $env:AZURE_CONFIG_DIR = $script:AzureConfigDirByRoot[$root]
            return
        }
    }
    # Only undo a value set here, so a manually set or inherited AZURE_CONFIG_DIR survives.
    if ($env:AZURE_CONFIG_DIR -and $env:AZURE_CONFIG_DIR -eq $script:AzureConfigDirAuto) {
        $env:AZURE_CONFIG_DIR = $null
    }
}

Set-AzureConfigForLocation
$previousLocationChangedAction = $ExecutionContext.SessionState.InvokeCommand.LocationChangedAction
$ExecutionContext.SessionState.InvokeCommand.LocationChangedAction = {
    Set-AzureConfigForLocation
    if ($previousLocationChangedAction) {
        & $previousLocationChangedAction @args
    }
}.GetNewClosure()
LocationChangedAction runs every time the location changes, so cd D:\repos\contoso\some-repo switches me to the Contoso sign-in, and leaving the folder clears the variable and takes me back to my personal one. The call at startup covers a terminal that opens straight into one of those folders.

It only clears a value it set itself, so an AZURE_CONFIG_DIR you set by hand (like in the example above), or one the terminal inherited, is left alone. And instead of replacing LocationChangedAction, it calls whatever handler was already registered, so it plays nicely with other modules that use it.

A nice side effect: anything started from that terminal inherits the variable. AzureCliCredential (and DefaultAzureCredential when it falls back to it) gets its token by running az, so an app I dotnet run from a Contoso repo authenticates as my Contoso account.

What about extensions

You might have noticed AZURE_EXTENSION_DIR at the top. By default, extensions are installed into cliextensions under the config folder. That means each new profile starts with no extensions at all, and you end up running az extension add for each profile and updating each one separately.

AZURE_EXTENSION_DIR overrides that location. I point it at the extensions folder of my primary profile, so I install an extension once and every profile has it.
$env:AZURE_CONFIG_DIR = "$HOME\.azure-contoso"

az extension list `
    --query "[].name" `
    --output tsv
# (nothing)

$env:AZURE_EXTENSION_DIR = "$HOME\.azure\cliextensions"

az extension list `
    --query "[].name" `
    --output tsv
# account
# containerapp
# resource-graph
# Omitted for brevity
Sharing is safe because extensions are just code; there are no accounts or tokens in there. If you need a client to stay on a specific extension version (say a preview build of containerapp), simply don't set AZURE_EXTENSION_DIR for that folder, and it gets its own extensions. Just remember the Bicep CLI that az bicep install downloads still goes into bin under each config folder.

What it doesn't cover

This is an Azure CLI thing, so tools that don't go through az won't follow it.
  • Azure PowerShell (the Az module) keeps its own context and doesn't read AZURE_CONFIG_DIR.
  • Azure Developer CLI (azd) has its own AZD_CONFIG_DIR.
  • Anything not started from that terminal (Visual Studio or VS Code opened from the Start menu, for example) never sees the variable.

Is this a standard approach?

The variable is. Microsoft documents AZURE_CONFIG_DIR, and even recommends giving each script its own config folder when you run az concurrently, to avoid write conflicts on the token cache. What the Azure CLI doesn't have is named profiles, so one folder per account is the way to get them. It has been working great for me.

More read:

Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment