Search code examples
azureazure-active-directorymulti-tenant

Switch tenants from the command line using Microsoft Azure with 2FA


I'm trying to switch tenants from the command line interface on my Windows 10 computer.

The answers provided here don't seem to work at all

I've also tried the following

az logout 
az login --use-device-code (because of 2FA) 
az login --username [my user name] -t [tenant id] 
az login --tenant [tenant id] 
az login --tenant [tenant name] 
az account set --subscription [subscription id] 

All these commands are completed successfully, however, when I run Get-AzSubscription command, I still see the tenentID of a different tenant I use than the one I want to log into.

How can one successfully switch tenants from the command line interface using Microsoft Azure with 2FA while developing cloud native apps on a Windows 10 PC?


Solution

  • You should not use Get-AzSubscription to get the subscription information.

    Get-AzSubscription is in Az.Accounts Powershell module.

    But az login is in Azure CLI module. Switching tenants using az login doesn't effect on the results of Get-AzSubscription.

    You can see the tenant information immediately after logging in using az login --allow-no-subscriptions --tenant xxxx.onmicrosoft.com.

    You can also use az account show to get the details of a subscription and the tenant information.