Connecting to Exchange Online using MFA and ISE without EXO PowerShell Module

PowerShell

How to connect to Exchange Online using MFA without the Exchange Online PowerShell Module. If you have MFA enabled for your Exchange Online Admin accounts (which of course, you should), you will probably know that you cannot connect to Exchange Online using the normal PowerShell console, as detailed here https://cloudrun.co.uk/powershell/connecting-to-office-365-using-powershell/

The usual method of connecting with MFA is to simply download and install the Exchange Online PowerShell module from the Exchange admin center (EAC), see here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps, and then connect using Connect-EXOPSSession -UserPrincipalName [email protected]. However, you may want to use the normal PowerShell console or ISE to run scripts, and often the ClickOnce package does not work due to IE security settings or other reasons so can be problematic to use. So here is how you can connect to Exchange Online using MFA.

  1. First, download and install the EXO PowerShell module from the EAC as above. We are not going to run it, but you do need to have it installed.
  2. Then simply use the following code to load the module into a normal console:
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

Import-PSSession $EXOSession

I’ve also built this into a script which will check if you have everthing installed, and only connect if it needs to. I save this and include this in any script that needs it using e.g. .\ConnectExchangeOnline.ps1.

#Script to connect to Exchange Online with MFA
#The Exchange Online PowerShell Module needs to be installed from the Exchange Admin Center > Hybrid

#Cleanup any old sessions
get-pssession | Where-Object {$_.ComputerName -eq "outlook.office365.com" -and $_.State -eq "Broken"} | remove-pssession

#Check if the Exchange Online PowerShell Module is installed
$EXO = Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse
if (-Not $EXO) {
Write-warning "Exchange Online Module is not installed, exiting."
}
else {
Write-Host "Exchange Online Module is installed"

if (Get-Module -Name "Microsoft.Exchange.Management.ExoPowershellModule") {
Write-Host "Exchange Online Module already loaded"
}
else {
Write-Host "Exchange PowerShell Module is not loaded, loading..."

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
Import-PSSession $EXOSession
}

if (get-pssession | Where-Object {$_.ComputerName -eq "outlook.office365.com" -and $_.State -eq "Opened"}) {
Write-Host "Exchange Online PowerShell session already exists"
}
else {
Write-host "Creating new Exchange Online PowerShell session"
$EXOSession = New-ExoPSSession
Import-PSSession $EXOSession -AllowClobber
}

}
Posted in PowerShell

Related Posts

1 Comment

  1. Pingback:Connecting to Office 365 using PowerShell - Cloudrun

Leave a Reply

Your email address will not be published. Required fields are marked *

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