Azure Cli how to change subscription default

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.

154321 次浏览

Please try the following:

azure account set -s {Subscription Id}

That should change the subscription.

For Azure CLI 2.0 (preview) I had to use

az account set --subscription <name or id>

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

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"

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.

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>

Azure CLI latest (2.39.0):

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


az account set (--subscription or -s) <id>

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