az cli subscription context

Set Your Azure CLI Context with New Subscription

By Andrew Harris
AD

The Azure CLI is a powerful command-line interface for managing Azure resources. Understanding and effectively using `az cli subscription context` is crucial for streamlining your workflow and avoiding subscription mix-ups. Let's delve into this essential aspect of Azure CLI.

The `az cli subscription context` command helps you switch between Azure subscriptions, a vital function when you're managing multiple subscriptions in a single Azure account or a multi-subscription environment.

Setting the Subscription Context

Before you start working with subscriptions, you need to authenticate and set the subscription context.

AD
Azure CLI Cheat Sheet — Resource Groups & VM Reference
Azure CLI Cheat Sheet — Resource Groups & VM Reference
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
an email message with the caption'your subription has been successfully corrected '
an email message with the caption'your subription has been successfully corrected '

You can sign in to Azure using the `az login` command:

```bash az login ```

Once logged in, you can list all available subscriptions with:

Types of subscription services - infographic
Types of subscription services - infographic
How Substack Works: Newsletters & Subscriptions
How Substack Works: Newsletters & Subscriptions
Digital Subscription Hub
Digital Subscription Hub
```bash az account list ```

To set the subscription context, use:

```bash az account set --subscription "subscription-name" ```

Replace "subscription-name" with the name of the subscription you want to use.

AD

Using Subscription IDs

Instead of using the subscription name, you can also use the subscription ID:

Subscription & Account
Subscription & Account
an image of a website page with different colors and font
an image of a website page with different colors and font
What are your thoughts on subscription-based eCommerce models?
What are your thoughts on subscription-based eCommerce models?
an image of a website page with the words turn your shoppers into subscibers
an image of a website page with the words turn your shoppers into subscibers
Angular CLI Cheat Sheet: Essential Commands for Angular 17+ | Sonu Sindhu posted on the topic | LinkedIn
Angular CLI Cheat Sheet: Essential Commands for Angular 17+ | Sonu Sindhu posted on the topic | LinkedIn
Software Subscriptions Are Something We Need To Get Used To
Software Subscriptions Are Something We Need To Get Used To
```bash az account set --subscription "subscription-id" ```

You can find the subscription ID in the Azure portal or by running `az account list` and matching the ID to the relevant subscription.

Switching Between Multiple Subscriptions

To switch between subscriptions swiftly, you can use the `az account set` command along with a script or a bash function. Here's an example of a bash function you can create:

AD
```bash function switch-to-subscription() { az account set --subscription "$1" } ```

This function allows you to switch subscriptions by simply calling `switch-to-subscription "subscription-name"`.

Update or change credit card information
Update or change credit card information
Cancelled Letter, Letter To Cancel Subscription, Canceled Sign, How To Renew Subscriptions, Cancel My Subscription Sign, Computer Restrictions Error, Subscription Cancellation Request, Business Law Legal Commentary, Cancel Subscription Meme
Cancelled Letter, Letter To Cancel Subscription, Canceled Sign, How To Renew Subscriptions, Cancel My Subscription Sign, Computer Restrictions Error, Subscription Cancellation Request, Business Law Legal Commentary, Cancel Subscription Meme
50% of Loyal Subscribers Are Young Consumers With Multiple Subscriptions
50% of Loyal Subscribers Are Young Consumers With Multiple Subscriptions
an image of a red and white text message with the words additional information on it
an image of a red and white text message with the words additional information on it
💻 CLI vs GUI Explained | Beginner's Computer Science Cheat Shee
💻 CLI vs GUI Explained | Beginner's Computer Science Cheat Shee
TOP 5 BENEFITS OF A SUBSCRIPTION MODEL & HOW TO UTILIZE
TOP 5 BENEFITS OF A SUBSCRIPTION MODEL & HOW TO UTILIZE
Use subscription templates
Use subscription templates
a screen shot of the purchase form for a subshortion website, which is also available on amazon
a screen shot of the purchase form for a subshortion website, which is also available on amazon
someone is stamping on a computer screen with the word subscibe over it
someone is stamping on a computer screen with the word subscibe over it
an info sheet with different types of speech bubbles and other things to describe in it
an info sheet with different types of speech bubbles and other things to describe in it
an image of some type of webpage with many different things on it's screen
an image of some type of webpage with many different things on it's screen
Cancelling a subscription on Timely
Cancelling a subscription on Timely

Verifying the Subscription Context

After setting the subscription context, you can verify it using:

```bash az account show ```

This command displays the current subscription's details, confirming you're working in the correct subscription.

AD

Checking the subscription context is good practice, especially when working in a multi-subscription environment, to prevent unintentional resource modifications in the wrong subscription.

view the Current Subscription

To quickly view the current subscription's name, you can use:

```bash az account show --query "name" -o tsv ```

This command returns only the subscription's name, making it ideal for display in terminal outputs or scripts.

Checking the Global Az CLI Context

Besides the subscription context, you can also check the global Azure CLI context using:

```bash az account show --query "{login:subscriptionId, tenantId, user.name, user.objectId}" ```

This command returns the current user's subscription ID, tenant ID, and Azure user name and object ID.

Regularly verifying and switching Azure CLI contexts ensures you're always working in the correct subscription, helping you maintain a clean and organized resource management workflow.

Remember, always be cautious when managing resources to avoid unintended modifications or deletions. Keeping a clear overview of your current subscription context contributes significantly to this. Now go and master your Azure CLI subscription management!

AD