How to connect to Exchange Online PowerShell in Microsoft 365
This guide shows how to install the Exchange Online PowerShell module, sign in with modern authentication and MFA, confirm the connection and sign out cleanly. It is written for Microsoft 365 administrators who need to run Exchange Online cmdlets for the first time.
Before you start
- Role: signing in only needs an account that is allowed to use PowerShell. What you can do afterwards depends on role-based access control. Microsoft lists the Exchange Administrator role in Microsoft Entra, or the Organization Management or Recipient Management role group in Exchange Online, for checking and changing PowerShell access, and explains how to find the permissions any cmdlet needs. Microsoft recommends the role with the fewest permissions over Global Administrator.
- Shell: Windows PowerShell 5.1 or PowerShell 7. Windows PowerShell 5.1 needs .NET Framework 4.7.2 or later (Windows 11 already has it). Recent module versions need a recent PowerShell 7, so check the supported-versions table in the Microsoft article if you use PowerShell 7.
- Module:
ExchangeOnlineManagementfrom the PowerShell Gallery. On Windows, REST-based connections also need the PowerShellGet and PackageManagement modules. - Network: your computer must reach Microsoft 365. Microsoft notes that TCP port 80 should be open too.
- Not covered: unattended scripts, which should use app-only authentication or a managed identity instead of a person's sign-in.
Step by step
Steps 1, 3, 4 and 5 only read or end the session. Step 2 changes your own computer and is marked CHANGE (your computer). Only the last step, which is optional, changes the tenant.
- Check your shell and any existing module (read-only).
If the second command finds nothing, the module is not installed yet.$PSVersionTable.PSVersion Get-InstalledModule ExchangeOnlineManagement | Format-List Name,Version,InstalledLocation - CHANGE (your computer): install or update the module. On Windows PowerShell 5.1, Microsoft also lists setting the execution policy to
RemoteSignedas a prerequisite:
UseSet-ExecutionPolicy RemoteSigned Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser Update-Module -Name ExchangeOnlineManagement -Scope CurrentUserInstall-Modulethe first time andUpdate-Moduleafterwards. - Sign in (read-only session). Replace the example address with your own admin account:
Enter your password in the sign-in window, then complete MFA when prompted. In PowerShell 7 the prompt opens in your default web browser. This uses modern authentication and works with MFA, and Basic authentication is not used.Import-Module ExchangeOnlineManagement Connect-ExchangeOnline -UserPrincipalName admin@contoso.onmicrosoft.comVariations from the Microsoft article:
- No browser on the machine (PowerShell 7):
Connect-ExchangeOnline -Device, then enter the code at the device sign-in page on another device. - Managing a customer tenant as a partner or guest: add
-DelegatedOrganization customer.onmicrosoft.com. - Sovereign clouds: add
-ExchangeEnvironmentName, for exampleO365USGovGCCHigh. Standard Microsoft 365 needs no value. - WAM-related errors on module 3.7.2 or later: add
-DisableWAM.
- No browser on the machine (PowerShell 7):
- Confirm the session and try a read-only cmdlet.
Get-ConnectionInformation Get-AcceptedDomain - Disconnect when you are finished.
Microsoft warns that closing the window without disconnecting can use up the sessions available to you until they expire.Disconnect-ExchangeOnline -Confirm:$false - CHANGE (tenant, only if sign-in is refused): re-enable PowerShell access for one user. Every account can use Exchange Online PowerShell by default, but an admin may have turned it off. First read the status (read-only):
If it showsGet-User -Identity "admin@contoso.onmicrosoft.com" | Format-List EXOModuleEnabledFalseand the account should have access, an administrator with the right role runs:
If you are locked out completely, Microsoft suggests creating a new admin account in the Microsoft 365 admin center and using it to restore access.Set-User -Identity admin@contoso.onmicrosoft.com -EXOModuleEnabled $true
Check it worked
These commands only read.
Get-ConnectionInformation
Get-AcceptedDomain
Get-User -Identity "admin@contoso.onmicrosoft.com" | Format-List EXOModuleEnabled
If Connect-ExchangeOnline finished without errors you are connected. Get-ConnectionInformation should list one connection with State Connected, your account under UserPrincipalName and a TokenStatus of Active. Get-AcceptedDomain should list the domains of your organisation, and EXOModuleEnabled should be True for the account you use.
Common mistakes
- Trying the old Basic authentication or OAuth-token
New-PSSessionmethods. Microsoft lists them for history only and they are no longer supported. - Closing the window instead of running
Disconnect-ExchangeOnline, which uses up available sessions. - Using an account whose profile path contains special PowerShell characters such as
$. Connect and disconnect commands can fail; use another account. - Missing PowerShellGet or PackageManagement on Windows, which causes errors such as
Update-ModuleManifestnot being recognised. - Importing other modules first. A
System.IdentityModel.Tokens.Jwtassembly error means a module conflict; connect in a new Windows PowerShell window before importing other modules. - Using a federated account when your identity provider is not publicly reachable. Microsoft says to use a non-federated account for that case.
- Disabling PowerShell access for everyone in one command and including admins. Microsoft warns that a bulk
Set-User -EXOModuleEnabled $falsecan lock out your administrators, so exclude them. - Running an old PowerShell 7 with a new module version. Newer module versions require newer PowerShell 7 builds.
- Running day-to-day work under Global Administrator when a narrower role would do.
How ConfigCobra helps
ConfigCobra is read-only: it never changes your Microsoft 365 tenant. It reads the tenant with read-only Microsoft Graph permissions, with no agents and no admin password sharing, and assesses it against the CIS Microsoft 365 Foundations Benchmark v7.0.0 (160 controls, 143 of them automated). The related checks below cover Exchange Online settings you will reach through this PowerShell session.
When a control fails, ConfigCobra can give you a copy-paste remediation script (PowerShell or admin portal steps) for that finding, and remediation impact analysis shows which users, mailboxes and policies a fix would affect before you apply it. The inventory covers mailboxes and has a built-in risk view for external mail forwarding.
Go further
- All guidesAdmin how-tos for Microsoft 365, Azure, Google Cloud and AWS
- Check libraryEvery CIS check, explained
- RemediationSee what a fix changes in your tenant before you apply it, then use the generated script
- Inventory & InsightsEvery user, app, device and resource in one inventory, with built-in risk views
See where your Microsoft 365 stands
A read-only assessment against the CIS benchmarks, prioritised so you know what to fix first.