PowerShell: Connect to Exchange Online – MFA Style

PowerShell: Connect to Exchange Online – MFA Style

I’ve been using my Exchange Online connection script for some time now, and I love it – however, one of the biggest failings of that PowerShell module is that it doesn’t support MFA, which is becoming more common as a requirement as we go along.

Thankfully, Microsoft has released a new PowerShell module for connecting to Exchange Online – works great, keeps itself updated, is able to do a bunch of different connections… what’s not to love?


The only thing it doesn’t do (which I ALWAYS want) is load natively in PowerShell ISE – so I recently sat down and updated my connection script to load the MFA module so that I could keep scripting the way I normally do.

If you’re using my other script or something like it, you don’t need many changes – I’ve basically updated it to prompt for the UPN you want to connect with, and then import the Exchange Online Modern Auth enabled module:

#Connect to Exchange Online

Write-Host “Connecting to Exchange Online” -ForegroundColor Green

$UPN = Read-Host “Enter the UPN of the user you want to connect with”

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+“\Apps\2.0\”) -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch “_none_”}|select -First 1)

$EXOSession = New-ExoPSSession -UserPrincipalName $UPN

Import-PSSession $EXOSession

The one piece I’m not quite happy with yet is that the MSOL / Azure AD module doesn’t pass along the credentials in any way that I’ve found, so you’ll still get a prompt to enter your username.


After that, though, authentication continues and you’re done.


It’s not completely seamless, but it at least does both connections in one script – which works for me. Also, since this connection script still checks to see if you’re connected before re-authenticating, you’ll at least only have to authenticate once per session as you’re working on your scripts.

If they update that module (or if I figure out the solution), I’ll come back here and post it. As before, I’ve posted the script in its entirety to GitHub – enjoy!

Hope this helps – leave a comment if you have a question, or if you find this useful!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.