Script to get mailbox size and number of items info for all users or a single one

This script has two parts in it and depending on the selection it will either run it against a single specified mailbox or all of them at once.

Note, running this script against a few hundred mailboxes takes several minutes. So if your company is large with thousands of mailboxes, expect it to take much longer.

As part of the output it lists the following parameters – a user name, number of items in the mailbox, its current size, and a maximum allowed size.

There is a downside of using this script when running it against all mailboxes. Because Get-MailboxStatistics outputs TotalItemSize as a string only, it makes it harder to search through and sort by. I wish Microsoft could change that and provide data in a number type format.

$answer = Read-Host "Would you like to get statistics for all users ( y / n )?"

While ("y","n" -notcontains $answer) {
	$answer = Read-Host "Would you like to get statistics for all users ( y / n )?"
}

If ($answer -eq 'y') {
	$collection = @()
	$mailboxes = Get-Mailbox -ResultSize unlimited
	ForEach ($mailbox in $mailboxes) {
		$mailboxstatobject = $mailbox | Get-MailboxStatistics

		$outObject = "" | Select "Full Name","Number of Items","Used Space","Limit"

		$outObject."Full Name" = $mailbox.DisplayName
		$outObject."Number of Items" = $mailboxstatobject.ItemCount
		$outObject."Used Space" = $mailboxstatobject.TotalItemSize
		$outObject."Limit" = $mailbox.ProhibitSendReceiveQuota

		$collection += $outObject
        
	}
    
	$collection | Out-GridView

	Remove-Variable * -ErrorAction SilentlyContinue
	
}
	
Else {
	$answer1 = Read-Host "User's email address OR n"
	While ("n" -notcontains $answer1) {
		Write-host `n
		$singlemailbox1 = Get-Mailbox -Identity $answer1
		
		Get-MailboxStatistics -Identity $singlemailbox1.DisplayName |
		Select @{
			Label="Full Name";
			Expression={$singlemailbox1.DisplayName}
		},
		
		@{
			Label="Number of items";
			Expression={$_.ItemCount}
		},
		
		@{
			Label="Used space";
			Expression={$_.TotalItemSize}
		},
		
		@{
			Label="Limit";
			Expression={$singlemailbox1.ProhibitSendReceiveQuota}
		} | Out-Host
		Write-host `n 
		
		$answer1 = Read-Host "User's email address OR n"
		
	}
		
	Remove-Variable * -ErrorAction SilentlyContinue
	
}

This Post Has 2 Comments

  1. Navin Patel

    That’s nice piece of work you have done.. Great..

    I got some good ideas from your script.

    Thanks, keep up the good work.

    1. Pavel Bludov

      Thank you sir!

Leave a Reply