Skip to main content
These scripts cover day-to-day Exchange Online administration tasks: reporting on mailbox sizes, managing distribution groups, and configuring mail flow rules. All scripts target Exchange Online via the ExchangeOnlineManagement module, though many cmdlets are compatible with on-premises Exchange Server as well.

Prerequisites

# Install the Exchange Online Management module
Install-Module -Name ExchangeOnlineManagement -Force

# Import the module
Import-Module ExchangeOnlineManagement

Connecting to Exchange Online

1

Install the module

Install-Module -Name ExchangeOnlineManagement -Force
2

Connect with your admin account

Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com
You will be prompted for MFA if your account requires it. After authenticating, your session is active for all subsequent Exchange cmdlets.
3

Verify the connection

# Confirm you can retrieve mailbox data
Get-Mailbox -ResultSize 5 | Select-Object DisplayName, PrimarySmtpAddress
4

Disconnect when finished

Disconnect-ExchangeOnline -Confirm:$false
Use a dedicated service account or Global Admin with appropriate Exchange roles. The minimum role for most read operations is View-Only Recipients.

Mailbox management

Common cmdlets for working with user and shared mailboxes.
# List all mailboxes
Get-Mailbox -ResultSize Unlimited

# Get a specific mailbox
Get-Mailbox -Identity user@yourdomain.com

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

# Get statistics for all mailboxes
Get-Mailbox -ResultSize Unlimited | 
    Get-MailboxStatistics | 
    Select-Object DisplayName, TotalItemSize, ItemCount |
    Sort-Object TotalItemSize -Descending

Mailbox size report

Generate a CSV report of all mailbox sizes across your tenant.
# Run the mailbox size report script
.\Exchange\Get-MailboxSizeReport.ps1 -ExportPath "C:\Reports"
The -ExportPath parameter specifies the directory where the CSV report is saved. The script enumerates all mailboxes, retrieves statistics for each, and exports the results.
# Manual equivalent — build the report inline
$report = Get-Mailbox -ResultSize Unlimited | ForEach-Object {
    $stats = Get-MailboxStatistics -Identity $_.PrimarySmtpAddress
    [PSCustomObject]@{
        DisplayName       = $_.DisplayName
        EmailAddress      = $_.PrimarySmtpAddress
        MailboxType       = $_.RecipientTypeDetails
        TotalItemSize     = $stats.TotalItemSize
        ItemCount         = $stats.ItemCount
        LastLogonTime     = $stats.LastLogonTime
    }
}

$report | Export-Csv -Path "C:\Reports\MailboxSizes.csv" -NoTypeInformation
Write-Host "Report saved to C:\Reports\MailboxSizes.csv"

Distribution groups

# List all distribution groups
Get-DistributionGroup -ResultSize Unlimited | 
    Select-Object DisplayName, PrimarySmtpAddress, GroupType

# Get members of a specific group
Get-DistributionGroupMember -Identity "it-team@yourdomain.com" | 
    Select-Object DisplayName, PrimarySmtpAddress

# Add a member to a distribution group
Add-DistributionGroupMember -Identity "it-team@yourdomain.com" `
    -Member "newuser@yourdomain.com"

# Remove a member from a distribution group
Remove-DistributionGroupMember -Identity "it-team@yourdomain.com" `
    -Member "leavinguser@yourdomain.com"

Mail flow rules

# List all transport rules (mail flow rules)
Get-TransportRule | Select-Object Name, State, Priority

# Get details of a specific rule
Get-TransportRule -Identity "Block External Forwarding" | 
    Format-List

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

# Enable a transport rule
Enable-TransportRule -Identity "Rule Name"
Changes to mail flow rules take effect immediately in Exchange Online. Test new rules using the audit mode (SetAuditSeverity action) before enabling enforcement.

Build docs developers (and LLMs) love