Thursday, 13 October 2016

Check Mailbox Size and Usage Report using Powershell

In this collaborative business world mail communication is one of the best platform to share messages and files with customers and colleague. So, we often required to check mailbox size and quota for every user's mailbox. In this post, I am going write Powershell script to find mailbox size and usage status and find users who are going to reach their storage quota.

We can use the Exchange Online powershell cmdlet Get-MailboxStatistics to get mailbox size, and other mailbox related statistics data. This cmdlet will be available for both Exchange On-Premises server and Exchange Online (Office 365). Before proceed, first we need to connect Remote 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 below command to get mailbox statistics for a single user.
Get-MailboxStatistics -Identity [username]
By default this command returns the DisplayName, ItemCount, StorageLimitStatus, and LastLogonTime fields for the specified user. But what we need is mailbox size, so we need to alter the command to return required fields.
Get-MailboxStatistics [username] | Select DisplayName, TotalItemSize,StorageLimitStatus, ItemCount
The field StorageLimitStatus indicates whether the user is above or below the storage limit. The field TotalItemSize shows the total size of a mailbox in bytes, we need to convert this size into MB or GB to make the value as user friendly.
Get-MailboxStatistics [username] | Select DisplayName, @{n=”Total Size (MB)”;e={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, StorageLimitStatus

Find Mailbox Size for all Office 365 users:

You can use the powershell cmdlet Get-Mailbox to get all the mailboxes and pipe the results into Get-MailboxStatistics cmdlet to get mailbox size for all users.
Get-Mailbox -ResultSize Unlimited  | Get-MailboxStatistics |
Select DisplayName, @{n=”Total Size (MB)”;e={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, StorageLimitStatus

Export Mailbox Size, Quota and Storage Limit Status Report to CSV:

The below powershell script export mailbox name, userprincipalname, current mailbox usage size (TotalItemSize), storage warning quota (IssueWarningQuota), maximum size limit (ProhibitSendQuota) and size limit status (StorageLimitStatus) to csv file
$mailboxes = Get-Mailbox -ResultSize Unlimited
$totalmbx = $mailboxes.Count
$i = 1 
$mailboxes | ForEach-Object {
$mbx = $_
$mbs = Get-MailboxStatistics $mbx.UserPrincipalName
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
if ($mbs.TotalItemSize -ne $null){
$size = [math]::Round(($mbs.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)
$size = 0 }

$Result += New-Object PSObject -property @{ 
Name = $mbx.DisplayName
UserPrincipalName = $mbx.UserPrincipalName
TotalSizeInMB = $size
StorageSizeLimit = $mbx.ProhibitSendQuota
StorageLimitStatus = $mbs.ProhibitSendQuota
$Result | Export-CSV "C:\\MailboxSizeReport.csv" -NoTypeInformation -Encoding UTF8


No comments:

Post a Comment