PowerShell; Report Mailbox and OneDrive Size per User

Microsoft’s 365 has a ton of built-in reports for almost every metric you can think of. I recently needed to show Exchange Online and OneDrive consumption in the same report for a migration project. I was surprised to find the data I needed was available in separate reporting functions, but not together.

I briefly considered using Power BI or Excel to extract the data from the two separate reports and combining the attributes into a new one. Ultimately, I decided PowerShell would be faster. The script below requires that the MS 365 modules be installed on the machine it is run from. This Microsoft article has instructions for installing them. https://learn.microsoft.com/en-us/microsoft-365/enterprise/connect-to-all-microsoft-365-services-in-a-single-windows-powershell-window?view=o365-worldwide

#Import Required Modules 
Import-Module ExchangeOnlineManagement
Import-Module Microsoft.Online.SharePoint.PowerShell

#Connect to the Tennant
Connect-ExchangeOnline
Connect-SpOService -Url https://URL to top level SharePoint Site 

#Retrieve Data
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, UserPrincipalName, ForwardingSmtpAddress, DeliverToMailboxAndForward, 
    @{N="MailboxSize"; E={(Get-MailboxStatistics -Identity $_.UserPrincipalName | Select-Object TotalItemSize).TotalItemSize}}, 
    @{N="MailboxLastLogon"; E={(Get-MailboxStatistics -Identity $_.UserPrincipalName | Select-Object LastLogonTime).LastLogonTime}}, 
    @{N="OneDriveUsedGB"; E={(Get-SPOSite -IncludePersonalSite $true -Limit all -Filter "Owner -like '$($_.UserPrincipalName)'" | Select-Object   StorageUsageCurrent).StorageUsageCurrent/1024}} |
Export-Csv -Path $env:userprofile\Documents\365_Use_Report.csv -NoTypeInformation

Leave a comment