153

I have 3 subscription in my Azure Account, I need to change the default subscription. When i Run the command:

azure account list 

I have this output:

enter image description here

I have tried to change the default or current subscription on this way, with no results...

azure config set subscription {{MyIdSubscription}}

Any ideas? Thanks.

Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
chemitaxis
  • 13,889
  • 17
  • 74
  • 125
  • An important tip! Be careful mixing Azure Shell and Powershell - eg. "az login" and "Connect-AzAccount" If you use "az login" it will not reflect on commands like Get-AzContext. So if you have powershell scripts that depend on Get-AzContext they'll fail. – dbroggy Jun 23 '20 at 21:07

7 Answers7

309

For Azure CLI 2.0 (preview) I had to use

az account set --subscription <name or id>
Ida
  • 3,417
  • 1
  • 11
  • 11
  • 2
    If you have just recently updated from 'Free Trial` to Pay-As-You-Go you may need to `az logout && az login` again to see the current subscriptions. Otherwise it won't know about your changed subscriptions. – Holger Brandl Nov 28 '18 at 10:50
  • Let me add only that if you want to set the default subscription in a bash script using a variable like AZURE_SUBSCRIPTION="MyAzureSubscription", you've to use the command az account set --subscription "${AZURE_SUBSCRIPTION}" to avoid some bash errors...you cannot evaulate it directly as az account set --subscription $AZURE_SUBSCRIPTION – Leo Sep 23 '20 at 12:04
  • 1
    If you want to know the subscription id use the command `az account list` and from the output use the `id` entry. – Soundararajan Jan 28 '21 at 05:26
32

Please try the following:

azure account set -s {Subscription Id}

That should change the subscription.

chwarr
  • 6,777
  • 1
  • 30
  • 57
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
23
1. List all the subscriptions you have

    az account list --output table
    
      Name             CloudName     SubscriptionId     State     IsDefault
    ---------------   ------------  ----------------  ---------  ----------
    AssociateProd      AzureCloud    xxxxxxxxxxxx       Enabled    False

2. Pick the subscription you want and use it in the command below.

    az account set --subscription <subscription_id>
Akash Yellappa
  • 2,126
  • 28
  • 21
10

Use id (subscription id) that is GUID, which will be listed when you did az login

enter image description here

And then execute the below command..

az account set --subscription fffde5cb-cccc-aaaa-eee-457c3292608e
Karthikeyan VK
  • 5,310
  • 3
  • 37
  • 50
8

Try in this way.it worked for me to set Azure PowerShell to a specific Azure Subscription

Set-AzContext -SubscriptionId "t666-e251-49ce-a1cd-5c3144"
Sachin Kalia
  • 1,027
  • 14
  • 24
7

Azure CLI latest (2.39.0):

az account set (--name or -n) <name>

az account set (--subscription or -s) <id>
brennfoster
  • 152
  • 1
  • 3
-1

I have created a wrapper around the Azure-CLI for this in a PiP Package, with colors.

pip install azure-account-switcher

note: it's dependent on azure-cli which downloads a lot of dependencies. So you should install in environment with azure-cli already present.

Example-usage

MeneerBij
  • 309
  • 2
  • 6