Azure Cli how to change subscription default
Asked Answered
E

8

171

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.

Entablement answered 20/7, 2016 at 7:36 Comment(1)
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.Bullfighter
T
352

For Azure CLI 2.0 (preview) I had to use

az account set --subscription <name or id>
Thistle answered 6/1, 2017 at 20:4 Comment(3)
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.Harlie
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_SUBSCRIPTIONCarlock
If you want to know the subscription id use the command az account list and from the output use the id entry.Pedroza
H
34

Please try the following:

azure account set -s {Subscription Id}

That should change the subscription.

Hiss answered 20/7, 2016 at 7:45 Comment(2)
Will it be also changing the subscription on the UI , in case a person is a part of multiple subscription ?Odo
I had to use az instead of azureChub
G
30
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>
Gathering answered 14/7, 2020 at 14:48 Comment(0)
B
12

Azure CLI latest (2.39.0):

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

az account set (--subscription or -s) <id>
Bessiebessy answered 17/8, 2022 at 15:26 Comment(0)
M
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
Monies answered 9/9, 2018 at 11:32 Comment(1)
az login is mandatory, before the az account set -s command even works! Thx :)Kero
U
9

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"
Uvarovite answered 24/9, 2019 at 7:30 Comment(1)
Note: Set-AzContext is Azure Powershell cmdlet, not Az CLI.Lurleen
M
0

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

Mayamayakovski answered 7/11, 2022 at 14:24 Comment(0)
B
0

An important tip for those that have used az account list but cannot find requried subscription among the listed ones, and the az account set does not accept the id returning

The subscription of 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx' doesn't exist in cloud 'AzureCloud'.

If that's the case, all you need to do is relogin to clear the cache:

az login, complete the procedure and the account list should be now updated (logout shoud not be needed)

Bukovina answered 16/1 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.