Managing Microsoft 365 (formerly Office 365) often requires PowerShell for bulk operations, automation, and accessing settings not available in the admin center. This guide covers the modern way to connect using the ExchangeOnlineManagement module, as well as connecting to other Microsoft 365 services.

Important: Microsoft deprecated Basic Authentication for Exchange Online in October 2022. The legacy New-PSSession method no longer works. Use the modern module method described below.

Prerequisites

  • Windows PowerShell 5.1 or PowerShell 7+
  • Microsoft 365 admin account
  • Internet connectivity
  • PowerShellGet module (included in Windows 10/11 and Server 2016+)

Installing the Required Modules

Exchange Online Management Module

# Install the module (run PowerShell as Administrator)
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber

# Or update if already installed
Update-Module -Name ExchangeOnlineManagement

# Verify installation
Get-Module -Name ExchangeOnlineManagement -ListAvailable

Other Useful Microsoft 365 Modules

# Microsoft Graph (replaces AzureAD and MSOnline modules)
Install-Module -Name Microsoft.Graph -Force

# Microsoft Teams
Install-Module -Name MicrosoftTeams -Force

# SharePoint Online
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force

Connecting to Exchange Online

Standard Connection (with MFA support)

# Import module
Import-Module ExchangeOnlineManagement

# Connect — opens a browser window for authentication
Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com

A browser window opens for authentication. Complete your password and MFA challenge.

Connect Without Browser Prompt

For automated scripts in environments where a browser is not available:

# Using certificate-based authentication (recommended for automation)
Connect-ExchangeOnline -CertificateThumbprint "THUMBPRINT" `
    -AppId "APP-ID" `
    -Organization "yourdomain.onmicrosoft.com"

Connect to a Specific Tenant (for partners/MSPs)

Connect-ExchangeOnline -UserPrincipalName admin@partner.com `
    -DelegatedOrganization "client-tenant.onmicrosoft.com"

Common Management Tasks

Once connected, you can run Exchange Online cmdlets:

Mailbox Management

# List all mailboxes
Get-EXOMailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySmtpAddress

# Get a specific mailbox
Get-EXOMailbox -Identity "user@yourdomain.com" | Format-List

# Get mailbox statistics (size, item count)
Get-EXOMailboxStatistics -Identity "user@yourdomain.com" | 
    Select-Object DisplayName, TotalItemSize, ItemCount

# Set mailbox properties
Set-Mailbox -Identity "user@yourdomain.com" -MaxReceiveSize 50MB

Distribution Groups

# List all distribution groups
Get-DistributionGroup -ResultSize Unlimited

# Get members of a group
Get-DistributionGroupMember -Identity "sales-team@yourdomain.com"

# Add a member
Add-DistributionGroupMember -Identity "sales-team" -Member "newuser@yourdomain.com"

Mail Flow Rules (Transport Rules)

# List all transport rules
Get-TransportRule | Select-Object Name, State, Priority

# Disable a rule
Disable-TransportRule -Identity "Rule Name"

Connecting to Other Microsoft 365 Services

Microsoft Graph (Users, Groups, Licenses)

Import-Module Microsoft.Graph

# Connect with specific permissions
Connect-MgGraph -Scopes "User.Read.All", "Group.ReadWrite.All"

# List all users
Get-MgUser -All | Select-Object DisplayName, UserPrincipalName, AccountEnabled

# Get license information
Get-MgSubscribedSku | Select-Object SkuPartNumber, ConsumedUnits, 
    @{N='Total';E={$_.PrepaidUnits.Enabled}}

Microsoft Teams

Import-Module MicrosoftTeams

Connect-MicrosoftTeams

# List all teams
Get-Team | Select-Object DisplayName, GroupId, Visibility

Disconnecting Sessions

Always disconnect when finished to free resources:

# Disconnect Exchange Online
Disconnect-ExchangeOnline -Confirm:$false

# Disconnect Microsoft Graph
Disconnect-MgGraph

# Disconnect Teams
Disconnect-MicrosoftTeams

Legacy Method (No Longer Works)

For historical reference, this was the old connection method. It stopped working in October 2022 when Microsoft disabled Basic Authentication:

# DEPRECATED — DO NOT USE
$cred = Get-Credential
$session = New-PSSession -ConfigurationName Microsoft.Exchange `
    -ConnectionUri https://ps.outlook.com/powershell/ `
    -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $session -Prefix o365

If you still have scripts using this method, upgrade them to use Connect-ExchangeOnline.

Troubleshooting

Module Not Found

# Update PowerShellGet first
Install-Module -Name PowerShellGet -Force
# Then retry installing ExchangeOnlineManagement

Access Denied

Ensure your account has the required admin roles:

  • Exchange Administrator for mailbox management
  • Global Administrator for full access
  • Security Administrator for compliance features

Connection Timeout Behind Proxy

Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com `
    -PSSessionOption (New-PSSessionOption -ProxyAccessType IEConfig)

Summary

The modern way to manage Microsoft 365 via PowerShell is the ExchangeOnlineManagement module with Connect-ExchangeOnline. It supports MFA, is actively maintained by Microsoft, and replaces the deprecated Basic Authentication method. For user and group management outside Exchange, use the Microsoft.Graph module.