# Set Your AzContext Interactively

# Azure Contexts in Powershell

If you are working with Azure, the probability of you having multiple accounts and subscriptions to work with are very high. If I look at myself, I have a personal account, a business account and my workaccount that I often use everyday. Deploying scripts through Powershell requires you to make sure you are in the right context. Especially with the rise of Azure Lighthouse, where you maybe have access to a lot of subscriptions through one account, a simple mistake is easily made.

# Switching between accounts

The workflow I previously used for executing Powershell scripts was to Disconnect all accounts, logging in with the right one and setting the right subscription with Set-AzContext. It works, but takes a lot of time. To reduce the time I need for switching between contexts, I wrote a simple script which works as follows:

  • It will list all your connected Contexts through Get-AzContext -List
  • You choose an account (No input defaults to 0)
  • It will list all the Enabled Subscriptions accessible through the selected account
  • You choose a subscription (No input defaults to 0)
  • The context will be set

# How to use it

To access the function everywhere, you can add it to you Powershell profile. By running $profile within powershell you can determine the location of your PowerShell Profile. Prefixing the variable with your favorite editor will open the profile directly. For example:

code $profile
1

# The code

Add the following code to your profile:

function azc {
    $allContexts = (Get-AzContext -List | Sort-Object Account)
    $contextArray = @()
    $contextEmailArray = @()
    $contextCount = 0

    Write-Host ""
    Foreach($context in $allContexts){
        if(!$contextEmailArray.Contains($context.Account.Id)){
            $contextArray += $context
            $contextEmailArray += $context.Account.Id
            Write-Host ("[" + $contextCount + "] - " + $context.Account)
            $contextCount++
        }
    }

    if (!($contextInput = Read-Host "Select Context")) { $contextInput = 0 }
    $setContext = Set-AzContext  -Context $contextArray[$contextInput]

    $allSubscriptions = (Get-AzSubscription | Where-Object {$_.State -eq "Enabled"} | Sort-Object Name)
    $subscriptionArray = @()
    $subscriptionCount = 0

    foreach($subscription in $allSubscriptions){
        $subscriptionArray += $subscription.Id
        Write-Host ("[" + $subscriptionCount + "] - " + $subscription.Name + " - " + $subscription.Id)
        $subscriptionCount++
    }

    if($subscriptionArray.Count -gt 1) {
        if (!($subscriptionInput = Read-Host "Select Subscription")) { $subscriptionInput = 0 }
    } else {
        $subscriptionInput = 0
    }

    Set-AzContext -SubscriptionId $subscriptionArray[$subscriptionInput]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

This is a quick sketch, but it works very well! If you think the code could more elegant, or you have ideas for functionality, I highly encourage you to improve it! The code can be found at: https://github.com/stefanpetter/InteractiveAzContext

Thank you for reading!