Azure PowerShell Az: List and Set Azure Subscription
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're executing commands against.
If you're logged in with an accocunt 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) thgat Azure PowerShell is scoped against to execute commands for:
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.
Notice that the Azure PowerShell
Azcommands 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
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:
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
tenantid for those subscriptions.
Set Azure Subscription to Target
After you've run the previous command and know either the
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
# 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
name for the Azure Subscription.
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.
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.