To list and set the Azure Subscription to run Azure CLI commands against is an important step in command-line scripting. To do this, there are a couple important commands used to list the Azure Subscriptions your login has access to, view which subscription the CLI is currently scoped to, and set / change the subscription the CLI is scoped to.
While these commands are generally pretty simple to execute, they are extremely important to use. If you don’t set the scope (or context) of the Azure CLI to the correct Azure Subscription then you might provision, delete, or modify resources in the wrong subscription. This would be a really big problem!
Show Selected Azure Subscription
Before running any Azure CLI commands, you need to verify that you have the CLI’s context set to the Azure Subscription you intend to run commands against. If your login only has access to a single Azure Subscription, then this isn’t a problem. However, if your login has access to multiple Azure Subscriptions, then this is a very important thing to do.
To view which Azure Subscription the Azure CLI’s context is currently set to target, run the following command:
az account show
Notice that the Azure CLI commands refer to the Azure Subscription as an
account. This will define the base (or prefix) command for working with Azure Subscriptions as
az account show command is executed, you’ll see a JSON response written to the terminal with information about the specific Azure Subscription the Azure CLI is currently set to work with:
List Azure Subscriptions
You first need to know some details about your Azure Subscriptions in order to update the Azure Subscription the Azure CLI’s context is set to. The specific detail you need to know are the
name of your Azure Subscriptions. When you use the
az account list Azure CLI command, the terminal response will be JSON that contains this information about all the Azure Subscriptions your current login has access to.
To view a list of all the Azure Subscriptions you have access to, run the following command:
az account list
This command will list out the details for all the Azure Subscriptions you have access to within Microsoft Azure regardless if you have access to 1, 2 or many more.
You can see the JSON output contains a few different values, like the
tenantIdof Azure AD, the
useryou’re logged in with, and the
cloudNamefor which type of Azure cloud that Azure Subscription resides in. While these are useful values for other uses, only the
idare necessary for verifying and setting which Azure Subscription you want to run Azure CLI commands against.
Set Azure Subscription to Target
Once you know which Azure Subscription you need to scope the Azure CLI context to, or which Azure Subscription you want to run commands against, then you will set the Azure CLI to use that subscription.
To set the Azure Subscription you want to target with Azure CLI commands, you will run the following command to tell it explicitly which subscription you wish to target:
# Set subscription by Id az account set --subscription XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX # Set subscription by Name az account set --subscription "Company Subscription"
Be sure to replace the placeholders in the above examples with the actual
name for the Azure Subscription to target that was returned from the previous
az account list command.
az account set command executed successfully, then the terminal will not return a response. So it’s important that you run the
az account show command afterwards to verify the Azure CLI is set to the correct subscription before running other commands.
If there is an error setting the Azure CLI to a specific subscription, then an error message will be returned. Here is an example error for specifying an Azure Subscription that does not exist.