Export Office 365 Users Mailbox Size to CSV using Powershell

Occasionally, we need to find how much space is used by every office 365 user uses in their mailbox. In this post, we will explore how to export mailbox size, mail items count, and the last logon date of all users in your O365 tenant using PowerShell. We can use the Exchange Online PowerShell cmdlet Get-MailboxStatistics to extract mailbox statistics data.

Before you start, install the Exchange Online PowerShell module and run the below command to connect Exchange Online PowerShell.

Connect-ExchangeOnline

Run the following command to get the mailbox size for a single Office 365 user:

Get-MailboxStatistics -Identity "[email protected]" | Select TotalItemSize

Export Office 365 users mailbox size, mails count and last logon date

Run the following PowerShell script to export all M365 users’ mailbox sizes to a CSV file.

$Result=@() 
$mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 0 
$mailboxes | ForEach-Object {
$i++
$mbx = $_
$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName
 
if ($mbs.LastLogonTime -eq $null){
$lt = "Never Logged In"
}else{
$lt = $mbs.LastLogonTime }
  
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
  
$Result += New-Object PSObject -property @{ 
UserPrincipalName = $mbx.UserPrincipalName
TotalSize_MB = [math]::Round(($mbs.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)
TotalMessages = $mbs.ItemCount
LastLogonTime = $lt }
}
$Result | Select UserPrincipalName, TotalSize_MB, TotalMessages, LastLogonTime |
Export-CSV "C:\Temp\O365-Mailbox-Statistics.csv" -NoTypeInformation -Encoding UTF8

CSV Output of Mailbox Statistics Report:

export office 365 users mailbox sizes to csv using powershell
Advertisement