Connect To 365 Exchange Online With PowerShell And 2 Factor Authentication

If you have 2 factor authentication enabled for your Office 365 Exchange Online administrator accounts and you try and use the standard way of connecting to Exchange Online using PowerShell then you will get an error. This is because it can’t handle the 2 factor authentication part or the authentication process.

To connect to Exchange Online with 2 factor authentication enabled you need to use the following PowerShell code:

$username = ""
Install-Module -Name ExchangeOnlineManagement
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName $username

Once you have run the above PowerShell code a screen will load which will allow you to login with your Microsoft account.

Hope you find this useful.