azure cli specify subscription

Azure CLI: Specify Subscription

By Rachel Young
AD

The Azure Command-Line Interface (CLI) is a powerful tool for managing Azure resources, enabling you to work efficiently with Azure from the command line or in scripts. A crucial aspect of using the Azure CLI is knowing how to specify the subscription you want to work with, as each user can be associated with multiple Azure subscriptions. This article will guide you through the process of specifying an Azure subscription using the CLI, ensuring you're always working with the intended resources.

Before diving into the commands, it's essential to understand that the Azure CLI automatically selects the subscription based on your Azure account's default settings. However, situations may arise when you need to work with a specific subscription, making it crucial to know how to switch between them.

Viewing Your Azure Subscriptions

Before specifying a subscription, you should familiarize yourself with the list of subscriptions available to you. The Azure CLI allows you to view all your subscriptions with a single command.

AD
Azure CLI Cheat Sheet — Resource Groups & VM Reference
Azure CLI Cheat Sheet — Resource Groups & VM Reference
a family tree is shown with several different types of items in the same area, including two
a family tree is shown with several different types of items in the same area, including two
an image of a computer screen with the word azure in blue and green on it
an image of a computer screen with the word azure in blue and green on it

The `az account list` command displays a list of all your Azure subscriptions. Here's a sample output:

```bash az account list [ { "cloudName": "AzureCloud", "id": "/subscriptions/12345678-1234-1234-1234-123456789012", "name": "Subscription 1", "state": "Enabled", "subscriptionId": "12345678-1234-1234-1234-123456789012", "tenantId": "09876543-4321-4321-4321-987654321098" }, { "cloudName": "AzureCloud", "id": "/subscriptions/98765432-3210-3210-3210-987654321098", "name": "Subscription 2", "state": "Enabled", "subscriptionId": "98765432-3210-3210-3210-987654321098", "tenantId": "09876543-4321-4321-4321-987654321098" } ] ```

Filtering Subscription List

To filter the list of subscriptions to display only specific details, you can use the `--query` parameter. For instance, to view only the subscription names, use:

GitHub - Azure-Samples/azure-cli-samples: Contains Azure CLI scripts samples used for documentation at https://docs.microsoft.com
GitHub - Azure-Samples/azure-cli-samples: Contains Azure CLI scripts samples used for documentation at https://docs.microsoft.com
Configuring Budget Alert In Azure
Configuring Budget Alert In Azure
the azure logo on a blue background with white letters and an arrow in the middle
the azure logo on a blue background with white letters and an arrow in the middle
```bash az account list --query "[].name" ```

Setting an Azure Subscription as Default

After viewing your subscriptions, you might want to set a specific subscription as the default for your current session. To do this, use the `az account set` command, specifying the subscription ID.

```bash az account set --subscription 12345678-1234-1234-1234-123456789012 ```

Switching Between Azure Subscriptions

In scenarios where you need to work with a different subscription temporarily, you can switch the active subscription without changing the default for your current session.

AD

To switch to a specific subscription, use the `az account set` command with the `--antage` parameter followed by the subscription name.

a blue and white poster with the words azure cli commands on it's side
a blue and white poster with the words azure cli commands on it's side
Multiple output in Azure Functions with C#
Multiple output in Azure Functions with C#
Azure Basics Explained Simply ☁️ | Microsoft Azure Cloud Services
Azure Basics Explained Simply ☁️ | Microsoft Azure Cloud Services
Azure Integration Services for Business Modernization
Azure Integration Services for Business Modernization
the azure cloud mindmap diagram
the azure cloud mindmap diagram
Azure Kubernetes Service (AKS) vs Google Kubernetes Engine (GKE)
Azure Kubernetes Service (AKS) vs Google Kubernetes Engine (GKE)
```bash az account set --antage "Subscription 1" ```

Listing Subscriptions in Use

To view the subscriptions you've used recently, you can list them using the `az account list-used` command. This command helps you keep track of your activities across different subscriptions.

Specifying Subscription in Azure CLI Commands

When running Azure CLI commands, you can always specify the subscription using the `--subscription` parameter. This approach ensures that the command is executed within the intended subscription.

AD

For example, to create a new web application in a specific subscription, use:

Enable your Azure subscription for Data Lake Store - Part One
Enable your Azure subscription for Data Lake Store - Part One
a poster with some notes on it that say azure clustering and how to use them
a poster with some notes on it that say azure clustering and how to use them
How To Become Azure Developer ?
How To Become Azure Developer ?
Extract Text From Images Using Computer Vision API and Azure Functions
Extract Text From Images Using Computer Vision API and Azure Functions
Expert Azure Consulting That Delivers Scalable and Secure Solutions
Expert Azure Consulting That Delivers Scalable and Secure Solutions
the microsoft azure logo on a white background
the microsoft azure logo on a white background
Microsoft Strategy Teardown
Microsoft Strategy Teardown
How to embed security in your Azure Data Science Project | Towards Data Science
How to embed security in your Azure Data Science Project | Towards Data Science
Integrate GPT Models with Azure OpenAI Service
Integrate GPT Models with Azure OpenAI Service
Create Azure Function in C# Script and Service Bus
Create Azure Function in C# Script and Service Bus
```bash az webapp create --resource-group myResourceGroup --name myApp --plan Free --subscription 12345678-1234-1234-1234-123456789012 ```

Using Subscription Aliases

For convenience, you can create aliases for your subscriptions, making it easier to work with them. To create an alias, use the `az account alias` command with the `--name` and `--subscription` parameters:

```bash az account alias --name myAlias --subscription 12345678-1234-1234-1234-123456789012 ```

Once the alias is created, you can use it to switch to the subscription or execute commands:

AD
```bash az account set --antage myAlias az webapp create --resource-group myResourceGroup --name myApp --plan Free --antage myAlias ```

Understanding how to specify an Azure subscription using the Azure CLI is crucial for managing your resources effectively. By mastering the commands discussed in this article, you'll be able to work efficiently across multiple subscriptions, ensuring that your Azure resources are well-organized and managed appropriately.

As you explore Azure's ecosystem, continue learning and leveraging the power of the Azure CLI to automate tasks, accelerate development, and maximize productivity. Happy coding!

AD