Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitManaging mailbox data is a core responsibility for Microsoft 365 administrators. While Microsoft Graph PowerShell continues to evolve, Exchange PowerShell remains the most reliable module for mailbox-related operations. In this article, we’ll walk through a script that fetches all mailboxes in your Microsoft Exchange Online environment, along with key details like size, licensing status, and more.
Note: While Graph PowerShell is improving steadily, it does not yet support complete mailbox administration. For mailbox-specific tasks, Exchange PowerShell is the best bet as of now.
# Connect to Exchange Online
Connect-ExchangeOnline -ErrorAction Stop
$mailboxes = Get-Mailbox -ResultSize Unlimited -ErrorAction Stop
$results = @()
foreach ($mb in $mailboxes) {
try {
$stats = Get-MailboxStatistics -Identity $mb.Identity -ErrorAction Stop
# Parse mailbox size from string (e.g., "2.53 GB (2,718,916,096 bytes)")
$sizeGB = if ($stats.TotalItemSize -match '([\d\.]+)\sGB') {
[math]::Round([double]$matches[1], 2)
} elseif ($stats.TotalItemSize -match '([\d\.]+)\sMB') {
[math]::Round(([double]$matches[1]) / 1024, 2)
} else {
0 # fallback value
}
# Determine mailbox license status
$licenseStatus = switch ($mb.RecipientTypeDetails) {
"UserMailbox" { "Licensed" }
"SharedMailbox" { "Shared (No License)" }
"RoomMailbox" { "Room (No License)" }
default { "Unlicensed" }
}
$results += [pscustomobject]@{
DisplayName = $mb.DisplayName
EmailAddress = $mb.PrimarySmtpAddress
UserPrincipalName = $mb.UserPrincipalName
MailboxSizeInGB = $sizeGB
ProhibitSendQuota = $mb.ProhibitSendQuota
LicenseStatus = $licenseStatus
}
}
catch {
continue # silently skip any errors
}
}
# Display in console
$results | Format-Table -AutoSize
# Export to CSV
$results | Export-Csv -Path "All-Mailboxes-Report.csv" -NoTypeInformation
Write-Host "`n✅ Clean mailbox report exported to All-Mailboxes-Report.csv"
This script connects to Microsoft Exchange Online using the Connect-ExchangeOnline cmdlet and fetches all mailboxes using Get-Mailbox.
You can expand this script by adding:
Error | Cause | Solution |
Get-Mailbox not recognized | Exchange module not loaded | Ensure ExchangeOnlineManagement is installed and connected |
ToBytes method not found | Object is deserialized | Use regex parsing as shown in the script |
Unknown values in size or license | Inconsistent property values | Script now handles missing/invalid data with fallbacks |
CSV not created | Path issue or access denied | Run as administrator or specify a different path |
This script provides a simple yet powerful way to audit all Microsoft 365 mailboxes in your tenant using Exchange PowerShell. With detailed output and a CSV export option, it's ideal for reporting, compliance, or cleanup tasks.
As Microsoft Graph PowerShell continues to mature, mailbox operations are still best handled via the Microsoft Exchange Online PowerShell module for the time being.
© m365corner.com. All Rights Reserved. Design by HTML Codex