Powershell to get Complete Mailbox Statistics in the Exchange 2007 Orginisation

Powershell has been around for a little while now, still, some people have yet to discover the power that it can deliver. Here we use a Powershell script command to export the complete details of the Mailbox in the Exchange Organization to a CSV file.

It's really hard to keep track of the all the mailbox in the Exchange organization. Some mailboxes grow very big and may be unidentified, some mailboxes are disabled and it stays in the server for long time with out deletion. Because of these and more scenarios, we may not able to find which is top 100 mailbox in the Organization.

This script helps to get all the details into the CSV file. We can schedule this script on daily basis.  Please find the copy of the script below. It pulls details  like Name, Mailbox size (MB), Mailcount, Mailbox limits, Account Active or disabled, CreatedDate, Lastlogon time, Last logofftime etc into the CSV file.

Please have a look at the script below, and feel free to ask any questions as comment below.

Get-Mailbox -ResultSize Unlimited |  select DisplayName, Alias, Database, PrimarySmtpAddress,@{name=IssuewarningQuota;expression={if ($_.IssueWarningQuota -match "UNLIMITED") {"-1"} else {$_.IssueWarningQuota.value.tomb() }}},@{name=ProhibitSendQuota;expression={if ($_.ProhibitSendQuota -match "UNLIMITED") {"-1"} else {$_.ProhibitSendQuota.value.tomb() }}},@{name=ProhibitSendReceiveQuota;expression={if ($_.ProhibitSendReceiveQuota -match "UNLIMITED") {"-1"} else {$_.ProhibitSendReceiveQuota.value.tomb() }}},WhenCreated  |export-csv C:\stats.csv
                      "DisplayName,Alias,MailboxSizeMB,ItemCount,PrimarysmtpAddress,IssueWarningQuotaMB,ProhibitSendQuotaMB,ProhibitSendReceiveQuotaMB,DatabaseName,CreationDate,LastLogonTime,LastLogoffTime,isActive" | out-file C:\MailstatsResult.csv
                      $csv = Import-csv -path "C:\stats.csv"
                      foreach($line in $csv)
                      	$MailboxStats =  Get-MailboxStatistics $Line.Alias | Select TotalItemSize,Itemcount,LastLogoffTime,LastLogonTime
                      	$L = "{0:N0}" -f $mailboxstats.totalitemsize.value.toMB()
                      	$Size = ""
                      	$Len = $L.Split(',')
                      	for ($i=0; $i -lt $Len.length; $i++)
                      			$Size = $Size +$Len[$i] 
                      		$adobjroot = [adsi]''
                      		$objdisabsearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)
                      		$objdisabsearcher.filter = "(&(objectCategory=Person)(objectClass=user)(mail= $Temp)(userAccountControl:1.2.840.113556.1.4.803:=2))"
                      		$resultdisabaccn = $objdisabsearcher.findone() | select path
                      	$actStatus = "1"
                      	$actStatus = "0" 
                      	$out =$Line.Displayname  + "," + $Line.Alias  + "," +  $Size + "," + $MailboxStats.ItemCount  + "," + $Line.PrimarySmtpAddress   + "," +  $Line.IssueWarningQuota + "," +  $Line.ProhibitSendQuota  + "," +  $Line.ProhibitSendReceiveQuota + "," + $Line.Database + "," +  $Line.WhenCreated + "," + $MailboxStats.LastLogonTime + "," + $MailboxStats.LastLogoffTime + "," + $actStatus
                      	$Out | Out-File C:\MailstatsResult.csv -Append

Open in new window

You can copy the above script to the text file and Save it it as MailboxStats.ps1 in C:\

You can then Execute the script from Exchange Management Shell. Open Exchange Management Shell on the Exchange Server and type C:\Mailboxstats.ps1 to start the execution.

Be aware, this script can take some time to complete depending on the number of mailbox's in the organization.

As an enhancement to the script we can always make the script to upload to a SQL database and we can query for the details and also start looking at some trending information.


Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.