How to get the current Azure subscription information every time you start a new PowerShell session

If you're like me and you open many PowerShell windows to work on Azure, this tip is for you.

Am I on the right Azure subscription?

At work and at home, I have to deal with a lot of Azure subscriptions. I also use a lot of PowerShell windows to perform the tasks I have to and it can be easy to forget what is the last subscription I selected.

A couple of releases ago, the Azure PowerShell team decided that every time you call Select-AzureSubscription it sets the subscription you specified as default in your user profile. This mean that if you call Select-AzureSubscription in a PowerShell session, every new session created after will use that subscription by default.

This can cause you problems if you have multiple subscriptions and you switch regularly between them. You can think that you are still in a development subscription but a colleague ask you 15 min ago to do something in a production environment and now you will point to this environment in all your new PS sessions. Good luck to explain your boss that you destroyed all VMs in environment B because you thought you were still pointing to environment A.

You can't do much about this behavior, except if you work with Azure profiles (aka memory profiles were introduced in version 0.8.15). What you can do though is just set a visual reminder displaying what is the default Azure subscription of your new PowerShell session.

Current Azure subscription visual reminder

With PowerShell you have profiles that enable you to execute custom logic and/or declare variables and functions every time a new PowerShell session is created. You have plenty of control over which host is the target because you can target all hosts or only PowerShell ISE or new PowerShell window for example. We will target all hosts.

To know where your profile file should reside, simply check the $profile variable in PowerShell. It will point in your My Documents folder, more specifically :
%UserProfile%\My Documents\WindowsPowerShell\profile.ps1

What you want to do in there is determine the current subscription and output it to the host.

$azureModule = Get-Module -ListAvailable Azure* | ? ModuleBase -match sdk
if($azureModule) {
    $subscription = Get-AzureSubscription -Current -ErrorAction SilentlyContinue
    if(-not $subscription) {
        $subscriptionMessage = "There is actually no selected Azure subscription."
    }
    else {
       $subscriptionMessage = ("Actually targeting Azure subscription: {0} - {1}." -f $subscription.SubscriptionName, $subscription.SubscriptionId)
    }
    Write-Host -BackgroundColor Black -ForegroundColor Yellow ('Using Azure PowerShell v{0}' -f $azureModule.Version)
    Write-Host -BackgroundColor Black -ForegroundColor Yellow $subscriptionMessage
}

Now every time you'll start a new PowerShell host session you will see this visual clue to remind you where you are pointing.

I wish you enjoy this little utility script.

Download the script here

Reference