How to connect to Exchange Online PowerShell via ISE with MFA the correct way

I had the issue that my ISE client timed out after 10 minutes and I could not re-logon to Exchange Online PowerShell and I had to start a new session each time. I asked on Twitter and got some great help from these guys, a big shout out to

It turned out I was doing it wrong. Here is how to do it right

  • Install the Exchange online PowerShell module for MFA
    • Go to Exchange Admin in portal.office.com
    • go to Hybrid and click download for the Exchange module
    • This will break in Chrome, so use Edge og IE
    • Note that you should run the installed module from time to time in order keep it up to date
  • Open PowerShell ISE or Visual Studio Code and use this code to connect to Exchange Online
#Import the module, requires that you are administrator and are able to run the script
Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse ).FullName | Select-Object -Last 1)
#connect specifying username, if you already have authenticated to another moduel, you actually do not have to authenticate
Connect-EXOPSSession -UserPrincipalName admin@contoso.com
#This will make sure when you need to reauthenticate after 1 hour that it uses existing token and you don't have to write password and stuff
$global:UserPrincipalName="admin@contoso.com"

Leave a comment

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