Microsoft 365 and PowerShell
Connect to Exchange Online PowerShell (Microsoft 365)
On this page
If you followed an older guide (including the original version of this one) that used New-PSSession to connect to Office 365, it no longer works. Microsoft has turned off Basic authentication in Exchange Online and retired those remote PowerShell sessions, so the old commands now fail with an access denied or connection error.
The replacement is the Exchange Online PowerShell module (ExchangeOnlineManagement) and a single command, Connect-ExchangeOnline. It works with multi-factor authentication (MFA) and doesn’t need you to store a password anywhere.
What you need
- An account with an Exchange admin role, such as Exchange Administrator or Global Administrator, for most mailbox commands.
- Windows PowerShell 5.1 (built into Windows 10 and 11) or PowerShell 7. The current module, version 3.10.0, needs PowerShell 7.6 or later if you use PowerShell 7.
1. Install the Exchange Online module
Open PowerShell and run:
Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser
Installing for the current user means you don’t need to run PowerShell as administrator. If you’re asked to install the NuGet provider or trust the PSGallery repository, answer Y.
In Windows PowerShell 5.1, you may also need to allow signed scripts to run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Already have the module? Keep it up to date, as Microsoft updates it regularly:
Update-Module -Name ExchangeOnlineManagement
2. Connect
Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.co.uk
Replace the address with your admin account. A Microsoft sign-in window opens, and you sign in and complete MFA as normal. When the prompt comes back, you’re connected.
To check, list a few mailboxes:
Get-EXOMailbox -ResultSize 10
You can now run any Exchange Online command, for example to set a mailbox’s out of office reply.
3. Disconnect when you’ve finished
Disconnect-ExchangeOnline -Confirm:$false
Always disconnect rather than just closing the window. There’s a limit on how many sessions an account can have open at once, and abandoned sessions count towards it until they time out.
Useful variations
- See your current connections:
Get-ConnectionInformationshows which tenant and account you’re connected as. Handy if you manage more than one. - No browser on the machine: in PowerShell 7, add
-Device. It gives you a code to enter at microsoft.com/devicelogin on another device. - Security & Compliance (Purview): the same module includes
Connect-IPPSSessionfor compliance searches, retention and DLP commands. - Scheduled scripts: don’t store a password in a script. Use certificate-based app-only authentication instead. Microsoft’s guide to app-only authentication for unattended scripts walks through the setup.
Turning off Focused Inbox (formerly Clutter)
Older guides, including one I wrote back in 2016, used Set-Clutter to stop Office 365 moving emails out of the inbox. Microsoft retired Clutter and replaced it with Focused Inbox, which splits the inbox into Focused and Other tabs. Once you’re connected, you can turn it off for one mailbox:
Set-FocusedInbox -Identity user@yourdomain.co.uk -FocusedInboxOn $false
Or for everyone in the organisation:
Set-OrganizationConfig -FocusedInboxOn $false
Outlook can take a while to pick up the change. Restarting Outlook usually speeds it up.
Troubleshooting
- “Connect-ExchangeOnline is not recognized”: the module isn’t installed or loaded. Install it as above, or load it with
Import-Module ExchangeOnlineManagement. - “Running scripts is disabled on this system”: set the execution policy to RemoteSigned, as in step 1.
- Errors about loading an assembly or authentication library: this often happens when the Microsoft Graph module is loaded in the same window. Use a fresh PowerShell window just for Exchange Online.
- “The term ‘Get-…’ is not recognized” after connecting: your account doesn’t have a role that includes that command. Check its admin roles in the Microsoft 365 admin centre.
For every option and parameter, see Microsoft’s Connect to Exchange Online PowerShell documentation.