12

I would like to retrieve the tenant name THIS-THING-HERE.onmicrosoft.com using Azure CLI. I can't really find in documentation.

EDIT: When I'm calling azure account list I don't get user name in the domain provided since I'm login with corporate email:

[
  {
    "cloudName": "AzureCloud",
    "id": "46ee2f65-7112-4c96-ad0a-3ff6ca22a615",
    "isDefault": true,
    "name": "Visual Studio Professional",
    "state": "Enabled",
    "tenantId": "1caf5d6b-58cb-40e6-88b3-eb9ab9c0c010",
    "user": {
      "name": "a.krajniak@avanade.com",
      "type": "user"
    }
  },
  {
    "cloudName": "AzureCloud",
    "id": "1efd84d6-173f-42cc-80db-7b2c17eb0edd",
    "isDefault": false,
    "name": "Microsoft Azure Enterprise",
    "state": "Enabled",
    "tenantId": "c48d02ad-7efd-4910-9b51-ebb7a4b75379",
    "user": {
      "name": "a.krajniak@avanade.com",
      "type": "user"
    }
  }
]
Dzior
  • 1,485
  • 1
  • 14
  • 30

5 Answers5

6

I use this:

az account list --query "[?isDefault].tenantId | [0]" --output tsv
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rafal Kozlowski
  • 224
  • 2
  • 2
5

You could use this command:

az ad signed-in-user show --query 'userPrincipalName' | cut -d '@' -f 2 | sed 's/\"//'

this will take user upn and take the last part

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
4

To get the primary domain of an Azure tenant using bash, az cli, curl and jq:

$ az login
$ AZURE_TOKEN_ID=$(az account get-access-token --resource-type ms-graph --query accessToken --output tsv)
$ curl --header "Authorization: Bearer ${AZURE_TOKEN_ID}" --request GET 'https://graph.microsoft.com/v1.0/domains' | jq -r '.value[] | select(.isDefault == true) | {id}[]'

The result will be something like:

mydomain.onmicrosoft.com
mpowrie
  • 603
  • 8
  • 14
4

This can be achieved in a one-liner thanks to the az rest subcommand:

This works for tenant users but also guests users and CSP administrators.

az rest --method get --url https://graph.microsoft.com/v1.0/domains --query 'value[?isDefault].id' -o tsv
Laurent
  • 51
  • 2
1

To retrieve tenant name:

In the Azure CLI (I use GNU/Linux):

$ azure login  # add "-e AzureChinaCloud" if you're using Azure China

This will ask you to login via https://aka.ms/devicelogin or https://aka.ms/deviceloginchina

    $ azure account show

 {
  "environmentName": "AzureCloud",
  "id": "aaabbbcccdd-eeff-gghh-iijj-abcdef256984",
  "isDefault": true,
  "name": "MSDN Subscription",
  "state": "Enabled",
  "tenantId": "ggzzttyyh-56rg-op4e-iixx-kiednd256",
  "user": {
    "cloudShellID": true,
    "name": "paul@xxx.onmicrosoft.com",
    "type": "user"
          }
 }

To get tenant ID:

az account list | jq -r '.[].tenantId'

To get tenant name:

az account list | jq -r '.[].user'.name

I hope it helps