Using the Azure PowerShell Az commands to select and list the Azure Subscriptions to run commands against are important tasks when scripting and automating Azure. There are just a few key commands that can be used to perform these tasks.

These commands are simple to execute, but important to use. If you forget to set the scope (or context) of the Azure PowerSell Az commands to the correct Azure Subscription, then you may end up provisioning or deleting resources in the wrong Azure Subscription. This would be a huge problem!

Show Selected Azure Subscription

When running Azure PowerShell Az commands, it’s important to verify that your command prompt is scoped to the correct Azure Subscription context. This will define which Azure Subscription you are executing commands against.

If you’re logged in with an account that only has access to a single Azure Subscription, then you don’t need to worry about it. However, if you have access to multiple Azure Subscriptions, then it’s very important that you set the context to the one you intend to run commands against.

Use the following command to view the current Azure Subscription (or context) that Azure PowerShell is scoped against to execute commands for:

Get-AzContext

When the Get-AzContext command is executed, the command prompt will return the primary information for the Azure Subscription that is currently selected for the Azure PowerShell context.

Azure PowerShell Az: List and Set Azure Subscription 1

Notice that the Azure PowerShell Az commands refer to the selected Azure Subscription as a context. This is the terminology the Azure PowerShell uses to refer to the currently selected Azure Subscription information that commands will be executed against.

List Azure Subscriptions

Before you can set the context of the Azure PowerShell Az commands, you need to know the id or name of the Azure Subscriptions you have access to. These are the values you will need to set the current context to a particular subscription.

You can use the following command to get a list of all the Azure Subscriptions your current login has access to:

Get-AzSubscription

If you only have access to a single Azure Subscription, then the output will only show that subscription. When you have access to multiple Azure Subscriptions, then this command will output the full list of subscriptions you have access to; including the name, id, and tenantid for those subscriptions.

Azure PowerShell Az: List and Set Azure Subscription 2

Set Azure Subscription to Target

After you’ve run the previous command and know either the name or id of the Azure Subscription to need to execute commands against, then you will need to actually set the Azure PowerShell context to that subscription.

To do this, you can use the following command, and pass it either the Azure Subscription name or id:

# Set subscription by Id
Set-AzContext -SubscriptionId "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
# Set subscription by Name
Set-AzContext -SubscriptionName "Company Subscription"

Be sure to replace the placeholder values within the above examples with the actual id and name for the Azure Subscription.

When the Set-AzContext command executes successfully, the command prompt will return the details for the Azure Subscription that is selected. This allows you to verify that the right subscription was in fact selected. After this, you can then begin executing commands, and switching subscriptions when ever necessary.

Azure PowerShell Az: List and Set Azure Subscription 3

An error message will be returned when an error occurs executing the Set-AzContext command. Here’s a screenshot of an example error message. In this case it’s an error stating "Please provide a valid tenant or a valid subscription" as the -SubscriptionName specified doesn’t match any Azure Subscriptions the current login has access to.

Azure PowerShell Az: List and Set Azure Subscription 4

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
Microsoft MVP HashiCorp Ambassador

Discover more from Build5Nines

Subscribe now to keep reading and get access to the full archive.

Continue reading