Set Azure Subscription with CLI
Managing Azure resources has never been simpler, thanks to the Azure CLI (Command Line Interface). One of the many tasks you can automate is managing Azure accounts, including subscriptions. Let's delve into how to set and manage subscriptions using the Azure CLI.
Before we start, ensure you have the Azure CLI installed on your machine. If not, you can download and install it from the official Microsoft website. Now, let's explore the world of Azure subscriptions through the CLI.
Setting Up Your Azure Account for CLI
Before you can manage subscriptions, you need to log in to your Azure account through the CLI. You can do this using the `az login` command. This command opens a browser session where you can authenticate your account.
If you have multiple subscriptions, Azure CLI will set your default subscription. To verify this, use the `az account show` command. If you want to change your default subscription, you can do so using `az account set --subscription
List Your Subscriptions
Once logged in, you can list all your subscriptions using the `az account list` command. This command provides a JSON output listing all your subscriptions with their IDs and names.
If you prefer a tabular format, use the `az account list --table` command. It displays the same information but in a neat, readable table.
Selecting a Subscription
Azure CLI allows you to select a specific subscription to use for commands. You can list your subscriptions with the `az account list` command, note down the subscription ID, and then set it with `az account set --subscription
To verify that you're using the correct subscription, use the `az account show` command. It displays the account details, including the currently selected subscription.
Managing Subscriptions
Now that we've covered how to list and select subscriptions, let's look at how to manage them, including creating and deleting subscriptions.
The Azure CLI doesn't directly support creating or deleting subscriptions. However, you can do this via the Azure portal or using Azure PowerShell. For predictable automation, consider using Azure Policy to manage subscriptions.
Viewing Subscription Details
You can view the details of a specific subscription using the `az account show --subscription
The `az account list --filter` command lets you filter subscriptions based on certain criteria, like display name or state.
Moving Resources Between Subscriptions
If you want to move resources from one subscription to another, you can do so using the `az resource convert` command. Here's an example: `az resource convert --id
Remember, you must have the appropriate permissions in both the source and destination subscriptions to move resources.
That's it! You now know how to set up and manage Azure subscriptions using the Azure CLI. This knowledge can greatly simplify your Azure resource management tasks. Happy coding!