Azure subscription switcher

Table of content

If you want to create your own or to contribute to an existing GitHub project you are on the right page.

This article presents a PowerShell script that can be used to quickly switch between subscriptions.

Managing subscriptions can be a challenge in any cloud journey. Here's a script to save you some time. 

Now can quickly switch between your Azure subscriptions by entering the listed index.

Forked and updated from: matthiasguentert/azure-subscription-switcher (github.com)

 1#azure-subscription-switcher
 2Function Switch-AzContext {
 3    if (-not (Get-Module -ListAvailable -Name Az.Accounts)) {
 4        Write-Host -ForegroundColor Red 'Az.Accounts PowerShell module not installed!'
 5        return
 6    }
 7
 8    Import-Module Az.Accounts -ErrorAction Stop
 9
10    try {
11        # Select only enabled subscriptions and avoid duplicated subscriptions if the user has multiple tenants enrolled with LightHouse
12        $SubscriptionList = Get-AzSubscription | Where-Object { $_.State -eq "Enabled" -and ($_.HomeTenantId -eq $_.TenantId)} | ConvertTo-Json | ConvertFrom-Json
13    } catch {
14        Write-Host -ForegroundColor Red "You have no context, please login first!"
15        return
16    }
17
18    try {
19        $SubscriptionActive = Get-AzContext | ConvertTo-Json | ConvertFrom-Json
20    } catch {
21        Write-Host -ForegroundColor Red "You have no subscription, please login first!"
22        return
23    }
24
25    $available = @()
26    $index = 1
27    $SubscriptionList | ForEach-Object {
28        $available += [PSCustomObject]@{
29            Active = if ($_.Id -eq $SubscriptionActive.Subscription.Id) { "===>" } else { $null }
30            Index = $index++
31            Subscription = $_.Name
32            SubscriptionId = $_.Id
33            State = $_.State
34            HomeTenantId = $_.HomeTenantId
35            Account = if ($_.Id -eq $SubscriptionActive.Subscription.Id) { $SubscriptionActive.Account.Id } else { $null }
36        }
37    }
38
39    $available | Format-Table -AutoSize
40
41    try {
42        [int]$userInput = Read-Host "Index (0 to quit)"
43
44        if ($userInput -eq 0) {
45            Write-Host -ForegroundColor Red 'Won''t switch Azure PowerShell context!'
46            return
47        } elseif ($userInput -lt 1 -or $userInput -gt $index-1) {
48            Write-Host -ForegroundColor Red "Input out of range"
49            return
50        }
51
52        $selection = $available | Where-Object { $_.Index -eq $userInput }
53        Write-Host -ForegroundColor Cyan 'Switching to:', $selection.Subscription
54        Set-AzContext -SubscriptionId $selection.SubscriptionId | Out-Null
55        Get-AzContext
56    } catch {
57        Write-Host -ForegroundColor Red "Invalid input, please enter a valid index!"
58    }
59}
60
61Clear-Host
62Switch-AzContext

Here is the script output:

Here is also a version with a user interface (Out-GridView):

 1#azure-subscription-switcher-with-outgridview
 2Function Switch-AzContext {
 3    if (-not (Get-Module -ListAvailable -Name Az.Accounts)) {
 4        Write-Host -ForegroundColor Red 'Az.Accounts PowerShell module not installed!'
 5        return
 6    }
 7
 8    Import-Module Az.Accounts -ErrorAction Stop
 9
10    try {
11        # Select only enabled subscriptions and avoid duplicated subscriptions if the user has multiple tenants enrolled with LightHouse
12        $SubscriptionList = Get-AzSubscription | Where-Object { $_.State -eq "Enabled" -and ($_.HomeTenantId -eq $_.TenantId)} | ConvertTo-Json | ConvertFrom-Json
13
14    } catch {
15        Write-Host -ForegroundColor Red "You have no context, please login first!"
16        return
17    }
18
19    try {
20        $SubscriptionActive = Get-AzContext | ConvertTo-Json | ConvertFrom-Json
21    } catch {
22        Write-Host -ForegroundColor Red "You have no subscription, please login first!"
23        return
24    }
25
26    $available = @()
27    $index = 1
28    $SubscriptionList | ForEach-Object {
29        $available += [PSCustomObject]@{
30            Active = if ($_.Id -eq $SubscriptionActive.Subscription.id) { "===>" } else { $null }
31            Index = $index++
32            Subscription = $_.Name
33            SubscriptionId = $_.Id
34            State = $_.State
35            HomeTenantId = $_.HomeTenantId
36            Account = if ($_.Id -eq $SubscriptionActive.Subscription.id) { $SubscriptionActive.Account.id } else { $null }
37        }
38    }
39
40    $selection = $available | Out-GridView -Title "Select a subscription. Found: $($SubscriptionList.count)" -OutputMode Single
41
42    try {
43        if (-not $selection) {
44            Write-Host -ForegroundColor Red "No subscription selected. Operation cancelled."
45            return
46        }
47
48        Write-Host -ForegroundColor Cyan 'Switching to:', $selection.Subscription
49        Set-AzContext -SubscriptionId $selection.SubscriptionId | Out-Null
50        Get-AzContext
51    } catch {
52        Write-Host -ForegroundColor Red "Invalid input, please enter a valid index!"
53    }
54}
55
56Clear-Host
57Switch-AzContext

Here is the script output:

Enjoy!