Selecting the right Subscription in PowerShell

When your organization has multiple Azure Subscriptions and you are entitled to work with them, selecting the correct subscription becomes an important thing to do.

Different approaches exist. I will have a look at the two most straight forward ones.

Using an argument to Connect-AzAccount

Connect-AzAccount holds specific attributes than can help you, namely SubscriptionName or SubscriptionId. If you know the Subscription name you want to connect to, issuing the following command directly connects to the correct subscription, after which you can start scripting:

Connect-AzAccount -SubscriptionName "Microsoft Partner Network"

To check to which subscription is linked, the command Get-AzContext can be issued.

Switching to another subscription in an active session

Switching to another Azure Subscription in an active session is also possible. This can be achieved by issuing the following command:

Set-AzContext -Subscription <subscription name> or <subscription id>

Useful PowerShell references

CommandletLink
Connect-AzAccount https://docs.microsoft.com/en-us/powershell/module/Az.Accounts/Connect-AzAccount?view=azps-3.0.0
Get-AzContext https://docs.microsoft.com/en-us/powershell/module/Az.Accounts/Get-AzContext?view=azps-3.0.0
https://docs.microsoft.com/en-us/powershell/module/Az.Accounts/Set-AzContext?view=azps-3.0.0

Enable-AzureRMAlias

Almost one year ago, the new Az Powershell module was released. The major change compared to the “older” AzureRM module is the fact that it is built on the .NET standard libraries, making it cross-platform compatible.
In addition, the nomenclature has been adjusted. AzureRM has been shorted to “Az”. No major updates or new features will be developed for the AzureRM module.

If you are late to the game, now is the time to start adjusting your scripts. December 2020 is announced as the date also bugs and security fixes won’t be published anymore, rendering the module not suitable for production anymore.

It is not recommended to run the AzureRM module and the Az module side-by-side. However, while you are re-authoring your scripts, luckily Microsoft offers you a “co-existence” method without requiring conflicting modules.. By issuing the commandlet Enable-AzureRMAlias , aliases will be created.

After using Enable-AzureRMAlias, the number of usable commands increases significantly:

If you want to enable the legacy commandlets for the localmachine, the “-Scope” parameter will help you out: https://docs.microsoft.com/en-us/powershell/module/az.accounts/enable-azurermalias?view=azps-3.0.0

Useful PowerShell references

CommandletLink
Enable-AzureRMAlias https://docs.microsoft.com/en-us/powershell/module/az.accounts/enable-azurermalias?view=azps-3.0.0
Disable-AzureRMAlias https://docs.microsoft.com/en-us/powershell/module/az.accounts/disable-azurermalias?view=azps-3.0.0
Uninstall-AzureARM https://docs.microsoft.com/en-us/powershell/module/az.accounts/uninstall-azurerm?view=azps-3.0.0