Managing multiple Azure accounts/subscriptions in PowerShell

Managing multiple Azure accounts/subscriptions in PowerShell

As one gets more involved with the Azure cloud environment and move beyond performing actions through the portal to the realms of automation using different tools and specifically powershell, one problem that usually crops up is how to manage the different accounts/subscriptions you have access to. If you're like me, you will want to keep some subscriptions apart especially your work and personal subscriptions.

Apart from this you will also want the ability to run scripts against them quickly and without having to log in and log out of accounts each time or moving between separate devices.

The context management cmdlets in Azure are a fantastic way to solve this problem and make your life easier.

To connect to one or more Azure Accounts, run the following command each time and provide your credentials for each of the accounts.

Connect-AzAccount -TenantId

Usually when the account contains multiple subscriptions, one subscription is chosen as the context at the beginning. You can use get-AzContext to get the current context. You can also use (get-AzContext).Name which will show you the context's name only. This useful when the name is long and the display gets truncated in powershell.

get-AzContext

(get-AzContext).Name #Display context name only.

To use another subscription as the current context, use the following command(s):

set-AzContext -subscriptionId <subscriptionID for your sub>

You can give this new context a name directly by providing a value for the name parameter. e.g.

Set-AzContext -SubscriptionId <subscriptionID for your sub>  -Name 'DevSub'
# This command will also give the context a memorable name
Get-AzSubscription -SubscriptionName 'MySubscriptionName' | Set-AzContext -Name 'MyContextName'

Use the following command to rename the context to something more memorable/easy to type

Rename-AzContext -SourceName <Name of your subscription> -TargetName <memorableName>
# Change 'ISRXL' to characters contained in your subscription's name

$sourceName = (get-AzContext -ListAvailable | where-object {$_.Name -like '*ISRXL*'}).name

Rename-AzContext -SourceName $sourceName -TargetName 'PersonalSub'

After renaming the contexts, use the get-AzContext -ListAvailable command again to see the Azure contexts available to you in powershell.

To use the contexts after they have been renamed to simpler, memorable names:

select-AzContext 'PersonalSub'

You can also save these context settings to a file:

# Save-AzContext -Path <path-to-folder-you-choose>
Save-AzContext -Path "C:\Users\Isrxl\Documents\AzureContext.json

Once the context settings have been saved to a file, they can be imported into another powershell session as follows:

Import-AzContext -Path "C:\Users\Isrxl\Documents\AzureContext.json

It is important to develop the habit of checking and switching your azure context before running any commands or scripts. In fact, I think it is a good practice to ensure that the first few lines of your scripts/script blocks checks that the right context is being used.

Useful Links:

https://docs.microsoft.com/en-us/powershell/azure/context-persistence?view=azps-7.3.2
https://docs.microsoft.com/en-us/powershell/module/az.accounts/clear-azcontext?view=azps-7.3.2
https://docs.microsoft.com/en-us/powershell/module/az.accounts/disconnect-azaccount?view=azps-7.3.2