Scripts & PS

How to Connect to Microsoft 365 with PowerShell

Published 14 December 2025 · 6 min read

Managing Microsoft 365 via PowerShell is essential for any M365 admin. Many tasks that take multiple clicks in the admin centre take one line of PowerShell, and bulk operations that would be impossible manually become straightforward scripts.

Required modules

📦
Key M365 PowerShell modules
Microsoft.GraphReplaces AzureAD and MSOnline
Entra ID, Intune, general M365 admin
ExchangeOnlineManagement
Exchange Online, mailboxes, mail flow
MicrosoftTeams
Teams policies, settings

Install modules

# Install all main modules (run as admin in PowerShell 7)
Install-Module Microsoft.Graph -Scope CurrentUser -Force
Install-Module ExchangeOnlineManagement -Scope CurrentUser -Force
Install-Module MicrosoftTeams -Scope CurrentUser -Force

Connect to Microsoft Graph

# Interactive login
Connect-MgGraph -Scopes "User.Read.All","Group.ReadWrite.All","Directory.ReadWrite.All"

# Check connected tenant
Get-MgContext

# Disconnect
Disconnect-MgGraph

Connect to Exchange Online

# Interactive login
Connect-ExchangeOnline -ShowBanner:$false

# Verify
Get-OrganizationConfig | Select Name

# Disconnect
Disconnect-ExchangeOnline -Confirm:$false

Connect to Teams

Connect-MicrosoftTeams
Get-CsTenant | Select DisplayName, TenantId
Disconnect-MicrosoftTeams

Frequently Asked Questions

Q: What is the difference between Microsoft.Graph and the old AzureAD module?

Microsoft.Graph is the modern replacement for the deprecated AzureAD and MSOnline modules. All new scripts should use Microsoft.Graph.

Q: Can I connect to all services in one script?

Yes. Call each Connect cmdlet at the start of your script. Each creates a separate session.

Q: How do I run M365 PowerShell scripts on a schedule?

Use an Entra ID app registration with a certificate for non-interactive authentication, then run via Windows Task Scheduler or Azure Automation.

Related Guides
-> Shared Mailboxes via PS-> OneDrive via PS-> Graph API Getting Started
// need intune set up properly?
Fixed-price Intune setup for UK businesses

App deployment, compliance policies, Conditional Access, and full documentation at a fixed price.

View Packages