PowerShell Basics: Getting the right Azure Subscription in Azure Cloud Shell

If you have multiple Microsoft Azure subscriptions, it can be challenging to keep track of them all. So PowerShell to the rescue! With the use of Azure PowerShell commands installed Azure Cloud Shell, I'll show you view your subscriptions, and change to different subscriptions available to you. From there, you can build resources in Azure using the default subscription.

Want to follow along in Azure Cloud Shell? Create a free trial account here and then browse to shell.azure.com to begin.

First, let's list out all of the Azure subscriptions you have. This will provide you the names, subscription IDs and more. Both of these can be used for specifying your subscription

To view the number of subscriptions in your login, use Get-AzSubscription | measure.

AzSub-01.pngGet-AzSubscription | measure

To list all of the subscriptions for your login & page through the list, use Get-AzSubscription | moreNotice you get the Name and Subscription ID that can be used

Azure-Sub-02.pngGet-AzSubscription | more

You can use copy and paste to select the Subscription ID or Subscription Name. Why do that when we can have PowerShell do the work for you. To views specific properties of a subscription, enter get-AzSubscription -SubscriptionName

Azure-Sub-03.pngget-AzSubscription -SubscriptionName

Knowing the subscription name or Id will allow you to work with resources by explicitly calling out the subscription information. But sometimes, you want all of your actions in the shell to be against the same subscription. First, you need to verify what subscription you are using by entering get-AzureRMContext to view the default subscription name.


This will tell you what Azure subscription is the default subscription, and used with all commands when a Subscription isn't specified.

To change the subscription context for use with all processes in the current user session, enter the following command:

Azure-Sub-06.pngSelect-AzureRmSubscription -SubscriptionName

Now let's verify this works as it should by creating a resource group, and then viewing all the resource groups in the default description. To create a resource group in the default subscription, enter New-AzureRmResourceGroup -Name -Location

Azure-Sub-07.pngNew-AzureRmResourceGroup -Name -Location

To view all the resource groups in the default subscription, enter Get-AzureRmResourceGroup


And that's it. All the commands you need to find your Azure subscriptions and start using them to build resources in your Azure subscription.


This article was originally published by Microsoft's ITOps Talk Blog. You can find the original article here.