Hossain Labs

Home / Blog / Exchange Online

PowerShell Basics for Office 365 Administration (Episode 2)

By Md Sajid Hossain4 May 2025Updated 1 December 20261 min read

Choose the module for the task before opening a session. This walkthrough covers the connection you need for directory queries and the separate connection used for Exchange administration.

Install the modules you need

For directory users, start with Microsoft Graph. Use ExchangeOnlineManagement for mailbox administration. Install the modules under the account that will run the examples.

PowerShell
Install-Module Microsoft.Graph.Users -Scope CurrentUser
Install-Module ExchangeOnlineManagement -Scope CurrentUser

Read a directory user

Sign in to the intended tenant. For this delegated example, request the permission that lets you read basic profiles. Your organization’s consent policy still applies.

PowerShell
Connect-MgGraph -Scopes "User.ReadBasic.All"
Get-MgContext | Select-Object Account, TenantId, Scopes
Get-MgUser -Top 5 | Select-Object DisplayName, UserPrincipalName

Read a mailbox

Connect separately to Exchange Online with an account that can read mailbox information. The Graph connection does not grant Exchange PowerShell permissions.

PowerShell
Connect-ExchangeOnline
Get-EXOMailbox -ResultSize 5 | Select-Object DisplayName, PrimarySmtpAddress

Close the sessions

PowerShell
Disconnect-ExchangeOnline -Confirm:$false
Disconnect-MgGraph

If an older script uses Connect-MsolService or Connect-AzureAD, identify the operation and migrate it before running the script. Neither legacy module is a prerequisite for the exercises here.

References

Md Sajid Hossain

Modern Workplace & Data Security Engineer in Dhaka. Microsoft 365, Purview, Entra ID and Exchange, from real tenants and support cases.

Work with me · LinkedIn