How to use the command 'az account' (with examples)
The ‘az account’ command is a part of the Azure CLI (Command-Line Interface) tool that enables users to manage Azure subscription information. With this command, users can perform various tasks such as listing subscriptions, setting an active subscription, listing supported regions, getting an access token, and displaying subscription details in different output formats.
Use case 1: Print a list of subscriptions for the logged-in account
Code:
az account list
Motivation: This use case allows users to quickly view all the subscriptions associated with their Azure account. It can be useful when users want to have an overview of their available subscriptions before performing any subscription-related operations.
Explanation:
The az account list
command lists all the subscriptions for the currently logged-in Azure account. It fetches the subscription information from the Azure Management API.
Example output:
[
{
"cloudName": "AzureCloud",
"id": "12345678-1234-1234-1234-123456789012",
"isDefault": true,
"name": "My Subscription",
"state": "Enabled",
"tenantId": "87654321-4321-4321-4321-210987654321",
"user": {
"name": "user@example.com",
"type": "user"
}
},
{
"cloudName": "AzureCloud",
"id": "98765432-9876-9876-9876-987654321098",
"isDefault": false,
"name": "Other Subscription",
"state": "Enabled",
"tenantId": "87654321-4321-4321-4321-210987654321",
"user": {
"name": "user@example.com",
"type": "user"
}
}
]
Use case 2: Set a subscription to be the currently active subscription
Code:
az account set --subscription subscription_id
Motivation: When working with multiple subscriptions, this use case allows users to switch between subscriptions and set a specific subscription as the currently active subscription. It ensures that all subsequent Azure CLI commands operate in the context of the selected subscription.
Explanation:
The az account set
command sets the specified subscription as the currently active subscription. It takes the --subscription
argument, which requires providing the subscription ID as the parameter.
Example output:
Setting "My Subscription" as the active subscription...
Use case 3: List supported regions for the currently active subscription
Code:
az account list-locations
Motivation: This use case helps users to identify the regions available for deployment within their active subscription. It provides valuable information when deciding where to deploy resources in Azure.
Explanation:
The az account list-locations
command lists all supported regions for the currently active subscription. It retrieves the region information from Azure.
Example output:
[
{
"displayName": "Australia East",
"id": "australiaeast",
"latitude": "-37.8136",
"longitude": "144.9631",
"name": "australiaeast",
"regionalDisplayName": "Australia East"
},
{
"displayName": "Australia Southeast",
"id": "australiasoutheast",
"latitude": "-33.8688",
"longitude": "151.2093",
"name": "australiasoutheast",
"regionalDisplayName": "Australia Southeast"
}
]
Use case 4: Print an access token to be used with MS Graph API
Code:
az account get-access-token --resource-type ms-graph
Motivation: In scenarios where users need to authenticate and access the Microsoft Graph API, this use case provides an access token that can be used for authentication purposes.
Explanation:
The az account get-access-token
command, when given the --resource-type
argument set to ms-graph
, retrieves an access token typically used with the Microsoft Graph API. The access token can be used to make authenticated requests to the Microsoft Graph API by including it in the HTTP Authorization
header.
Example output:
{
"accessToken": "<access_token>",
"expiresOn": "2022-12-31T08:01:00+00:00",
"resource": "https://graph.microsoft.com",
"tokenType": "Bearer"
}
Use case 5: Print details of the currently active subscription in a specific format
Code:
az account show --output json|tsv|table|yaml
Motivation: This use case allows users to retrieve detailed information about their currently active subscription and display it in their preferred output format. It provides flexibility in choosing the format that best suits their needs.
Explanation:
The az account show
command displays details of the currently active subscription. It supports multiple output formats controlled by the --output
argument. Users can choose to display the output in JSON, TSV (Tab-Separated Values), table, or YAML format.
Example output (JSON format):
{
"environmentName": "AzureCloud",
"homeTenantId": "87654321-4321-4321-4321-210987654321",
"id": "12345678-1234-1234-1234-123456789012",
"isDefault": true,
"managedByTenants": [],
"name": "My Subscription",
"state": "Enabled",
"tenantId": "87654321-4321-4321-4321-210987654321",
"user": {
"cloudShellID": true,
"name": "user@example.com",
"type": "user"
}
}
Conclusion:
The ‘az account’ command is a powerful tool for managing Azure subscription information. With various subcommands, it enables users to list subscriptions, set an active subscription, list supported regions, obtain access tokens, and display subscription details in different output formats. These use cases provide users with the necessary control and flexibility to effectively manage their Azure subscriptions and related tasks.