How to Use the Command 'az account' (with Examples)
The az account
command is part of Azure CLI, which is a set of commands used to manage Azure resources directly from the command line. This specific command is designed to manage your Azure subscription information. Whether you’re managing multiple subscriptions or need to configure your settings, the az account
command lets you seamlessly handle subscription-related configurations. Below are several use cases with examples to demonstrate how this command can be utilized effectively.
Use Case 1: List All Subscriptions for the Logged in Account
Code:
az account list
Motivation: Listing all subscriptions is particularly useful when you are managing multiple Azure subscriptions under a single account. This is common in businesses where different projects, environments, or clients are organized into separate subscriptions. By knowing all active subscriptions, you can efficiently switch, monitor, or update as needed.
Explanation:
az
: This is the Azure CLI prefix used to run commands related to Azure.account
: This specifies that we are executing a command related to Azure account management.list
: This argument requests a listing of all Azure subscriptions associated with the currently logged-in account.
Example Output:
[
{
"cloudName": "AzureCloud",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
"isDefault": true,
"name": "My Azure Subscription",
"state": "Enabled",
"tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"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: Setting a subscription as the currently active one is crucial for targeting specific Azure operations to the desired subscription. This helps in ensuring that commands executed subsequently will apply to the intended subscription, preventing accidental updates or deployments to the wrong environment.
Explanation:
az account set
: This initiates the process of specifying which subscription should be marked as active.--subscription
: This flag specifies which subscription to set. You must replacesubscription_id
with the actual ID of the subscription you want to activate.
Example Output: This command will not return any output if successful, indicating that the subscription has been set as the active one.
Use Case 3: List Supported Regions for the Currently Active Subscription
Code:
az account list-locations
Motivation: Identifying supported regions for a subscription is important when you need to deploy resources closer to your user base or data sources for improved performance. Not all resources are available in every region, so it’s crucial to check this before finalizing your deployment plans.
Explanation:
az account list-locations
: This command lists all geographical regions where you can deploy Azure resources under the active subscription.
Example Output:
[
{
"displayName": "East US",
"id": "/subscriptions/{subscription-id}/locations/eastus",
"latitude": "37.3719",
"longitude": "-79.8164",
"name": "eastus"
},
...
]
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: Azure administrators often require access tokens to interact with various Microsoft APIs, including Microsoft Graph API. Generating an access token allows for seamless integration and communication with these APIs, enabling automation and extended functionality in managing Azure resources.
Explanation:
az account get-access-token
: This command generates an access token for a specified resource.--resource-type ms-graph
: This specifies that the access token is intended for use with Microsoft Graph API.
Example Output:
{
"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUz...",
...
}
Use Case 5: Print Details of the Currently Active Subscription in a Specific Format
Code:
az account show --output json|tsv|table|yaml
Motivation: Printing subscription details in various formats aids in documentation, reporting, and integration with other systems. You might need different formats to feed into scripts, dashboards, or simply for readability and presentation purposes.
Explanation:
az account show
: This command provides details about the current subscription.--output
: Specifies the format to use for displaying subscription information. Options includejson
,tsv
,table
, andyaml
.
Example Output (JSON Format):
{
"environmentName": "AzureCloud",
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
"isDefault": true,
"name": "My Azure Subscription",
"state": "Enabled",
"tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"user": {
"name": "user@example.com",
"type": "user"
}
}
Conclusion:
The az account
command offers powerful options for Azure subscription management directly through the command line. Whether you need to switch between subscriptions, list available regions, or fetch detailed subscription information in a particular format, the versatility of this command is designed to meet the diverse needs of Azure Administrators and developers working with Azure resources.