Friday, 1 June 2018

Export Office 365 Users Mailbox Size to CSV using Powershell

Occasionally we need to get how much space is used by every office 365 user in their mailbox. In this post, I am going to share powershell script to export mailbox size, mail items count and last logon date of all users in your O365 tenant. We can use the Exchange Online powershell cmdlet Get-MailboxStatistics to extract mailbox statistics data.

Before proceed, first we need to connect Exchange Online powershel module by running below command:
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
Run the following command to get mailbox size for a single Office 365 user:
Get-MailboxStatistics -Identity "" | Select TotalItemSize

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

Run the following powershell script to export all o365 users mailbox sizes to CSV file.
$mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1 
$mailboxes | ForEach-Object {
$mbx = $_
$mbs = Get-MailboxStatistics -Identity $mbx.UserPrincipalName

if ($mbs.LastLogonTime -eq $null){
$lt = "Never Logged In"
$lt = $mbs.LastLogonTime }
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
$Result += New-Object PSObject -property @{ 
UserPrincipalName = $mbx.UserPrincipalName
TotalSize = $mbs.TotalItemSize
TotalMessages = $mbs.ItemCount
LastLogonTime = $lt }
$Result | Select UserPrincipalName, TotalSize, TotalMessages, LastLogonTime |
Export-CSV "C:\\O365-Mailbox-Statistics.csv" -NoTypeInformation -Encoding UTF8

CSV Output of Mailbox Statistics Report:

export office 365 users mailbox sizes to csv using powershell


1 comment:

  1. This is great! One thing that would be great is if they were all in the same unit size e.g. GB so it is easy to add them and get the total.