PowerShell Basics: Getting the right Azure Subscription in Azure Cloud Shell
Published Oct 22 2018 11:00 PM 26.7K Views
Microsoft

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 how to 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.

Get-AzSubscription | measureGet-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 

Get-AzSubscription | moreGet-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 <Insert Subscription Name>

get-AzSubscription -SubscriptionName <Insert Subscription Name>get-AzSubscription -SubscriptionName <Insert Subscription Name>

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.

get-AzureRMContextget-AzureRMContext

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:

Select-AzureRmSubscription -SubscriptionName <Insert Subscription Name>Select-AzureRmSubscription -SubscriptionName <Insert Subscription Name>

 

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 <Resource Group Name> -Location <Resource Location>

New-AzureRmResourceGroup -Name <Resource Group Name> -Location <Resource Location>New-AzureRmResourceGroup -Name <Resource Group Name> -Location <Resource Location>

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

Get-AzureRmResourceGroupGet-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.

 

1 Comment
Version history
Last update:
‎Oct 23 2018 10:58 AM
Updated by: