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.

Notice that the Azure PowerShell
Az
commands refer to the selected Azure Subscription as acontext
. 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.

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.

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.

This happened to me during some Azure training. While following labs, I created resources in my subscription instead of the provided “Azure Pass – Sponsorship.”
I also realized I had to set this separately for the Azure CLI using “az account set” for the portions of the labs using those commands. I wrote up my experiences at https://kevinhakanson.com/2020-01-08-setting-subscription-used-inside-azure-cloud-shell
I see you have posted about using the Azure CLI in a separate post. Maybe cross-link them?