26

I'm trying to use az against my Azure account. My account has two directories: one for personal (default) and one for business. I need to "switch to" the business directory so that az has access to the correct resources. However, I cannot find any way to achieve this via the command line, so when I do az group list I see the resource groups from my personal directory, not the business one.

How can I switch Azure directory from the CLI?

me--
  • 1,978
  • 1
  • 22
  • 42

3 Answers3

47

Subscription and directory is not the same. You can have access to several subscriptions in your work directory for example.

To login to a different (non-default) directory, use the --tenant option with the az login command, passing the FQDN for the directory, e.g.

az login --tenant yourdir.onmicrosoft.com

You can find the FQDN in Azure Portal when listing the directories.

When logged into a directory, you can see list of all your available subscriptions.

List of Directories in Azure Portal

velsietis
  • 73
  • 6
Marki555
  • 6,434
  • 3
  • 37
  • 59
  • 7
    Also note you may need to add param `--allow-no-subscriptions` in case there is no subscription for the directory. So use like this: `az login --tenant yourdir.onmicrosoft.com --allow-no-subscriptions`. E.g. we have directory for Azure AD B2C created and seems to require this, but not sure why. – user232548 Oct 09 '20 at 13:03
  • just wanted to say that the above comment saved me :) thanks to both of you! – JBoothUA Feb 20 '23 at 15:53
4
# List of the tenants:
az account tenant list
[
  {
    "id": "/tenants/91358f27-xxxx-xxxxxxxxxxx",
    "tenantId": "91358f27-xxxx-xxxxxxxxxxx"
  },
  {
    "id": "/tenants/cf39b7bf-xxxx-xxxxxxxxxxx",
    "tenantId": "cf39b7bf-xxxx-xxxxxxxxxxx"
  }
]

# Select the tenant ID:
az login --tenant cf39b7bf-xxxx-xxxxxxxxxxx --allow-no-subscriptions

# Set a validated subscription:
az account set --subscription "Pago por uso"

# Verify
az account list -o table
1

Ugh, nevermind. For some reason the CLI calls them subscriptions when the portal calls them directories. So I needed az account set --subscription $SUBSCRIPTION_ID

me--
  • 1,978
  • 1
  • 22
  • 42
  • 1
    This way you only change the active subscription in your default directory. You need to specify the directory during `az login` as shown in my answer. – Marki555 Sep 19 '19 at 08:45