🔧 New: User Management Graph PowerShell Toolkit

Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.

🚀 Launch Toolkit

Audit Microsoft Exchange Online Mailboxes with PowerShell

Managing 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.


i) The Script

# 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"
                            

ii) How the Script Works

This script connects to Microsoft Exchange Online using the Connect-ExchangeOnline cmdlet and fetches all mailboxes using Get-Mailbox.

  • Mailbox statistics like size are fetched using Get-MailboxStatistics.
  • Mailbox size is extracted from the TotalItemSize string using a regex pattern and converted to GB.
  • The license status is inferred based on mailbox type (user, shared, room).
  • A formatted table is displayed in the console and also exported to a CSV file.

iii) Further Enhancing the Script

You can expand this script by adding:

  • Last logon timestamp using $stats.LastLogonTime
  • Archive mailbox size using Get-MailboxStatistics -Archive
  • Mailbox item count via $stats.ItemCount
  • Filters to retrieve inactive, shared, or over-sized mailboxes
  • Send email alerts if mailbox size exceeds a threshold

iv) Possible Errors & Solutions

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

v) Conclusion

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.


Graph PowerShell Explorer Widget

20 Graph PowerShell cmdlets with easily accessible "working" examples.


Permission Required

Example:


                


                


                

© m365corner.com. All Rights Reserved. Design by HTML Codex