đź”§ 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

Fetch User Mailboxes with Size and License Details using Exchange PowerShell

As a Microsoft 365 administrator, keeping track of mailbox storage and licensing is critical. While Graph PowerShell continues to evolve, Exchange PowerShell remains the most reliable tool for comprehensive Microsoft Exchange mailbox administration.

This article walks you through a PowerShell script that retrieves only user mailboxes from your tenant and lists key information including mailbox size, license status, and quotas. A CSV export option is also included for reporting purposes.


The Script

# Connect to Exchange Online
Connect-ExchangeOnline -ErrorAction Stop
                                
# Fetch only User Mailboxes
$userMailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited -ErrorAction Stop
$results = @()
                                
foreach ($mb in $userMailboxes) {
    try {
        $stats = Get-MailboxStatistics -Identity $mb.Identity -ErrorAction Stop

        # Extract mailbox size in GB
        $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 if size not parsable
        }
                                        
        # License status - all user mailboxes assumed licensed
        $licenseStatus = "Licensed"

        $results += [pscustomobject]@{
            DisplayName         = $mb.DisplayName
            EmailAddress        = $mb.PrimarySmtpAddress
            UserPrincipalName   = $mb.UserPrincipalName
            MailboxSizeInGB     = $sizeGB
            ProhibitSendQuota   = $mb.ProhibitSendQuota
            LicenseStatus       = $licenseStatus
        }
    }
    catch {
        continue  # Skip problematic mailboxes
    }
}
                                
# Display results in console
$results | Format-Table -AutoSize
                                
# Export to CSV
$csvPath = "User-Mailboxes-Report.csv"
$results | Export-Csv -Path $csvPath -NoTypeInformation
                                
Write-Host "`nâś… Report generated: $csvPath"
                            

How the Script Works

  1. Connects to Exchange Online using Connect-ExchangeOnline.
  2. Filters out only UserMailbox types using -RecipientTypeDetails.
  3. Uses Get-MailboxStatistics to fetch mailbox sizes.
  4. Parses the size string and converts MB to GB when necessary.
  5. Sets the License Status to "Licensed" for all user mailboxes (customizable).
  6. Compiles the results into a PowerShell object array.
  7. Displays the table in the terminal and exports a report to User-Mailboxes-Report.csv.

Further Enhancing the Script

Here are a few suggestions to take this script even further:

  • Validate license status using Get-MgUserLicenseDetail (Graph module) or Get-MsolUser.
  • Include additional mailbox properties like archive status, item count, last logon time, etc.
  • Add filters for large mailboxes or those nearing quota limits.
  • Email the report to administrators via SMTP automation.

Possible Errors & Solutions

Error Cause Solution
Get-Mailbox : The term is not recognized Exchange Online module not imported Run Import-Module ExchangeOnlineManagement or reinstall the module
Connect-ExchangeOnline not found Module not installed Install using Install-Module ExchangeOnlineManagement
ToBytes() method not found Deserialization of TotalItemSize Use regex to parse mailbox size string instead of .ToBytes() method
Access Denied Insufficient permissions Ensure you're a Global Administrator or Exchange Administrator

Conclusion

This script is a must-have in any Microsoft 365 administrator’s toolkit. It uses Exchange PowerShell to efficiently gather vital details about user mailboxes, including mailbox sizes, quotas, and license status. With built-in error handling and a CSV export option, this script simplifies monitoring and reporting.

While Microsoft Graph PowerShell is the future, it still lacks certain administrative capabilities—especially around mailbox management. Until that gap is fully bridged, Exchange PowerShell remains the go-to option for anything mailbox-related in Microsoft Exchange Online.


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