This script could be used to get mailbox and mailbox database statistics in Microsoft Exchange 2010. You can use this script to get different properties based on your needs.
In a real world, IT administrators may want to get mailbox and mailbox database statistics for their production environment. By this way, they can closely monitor mailbox size growth and the database growth. They can also use this script for change verification after modifying some properties of mailboxes and mailbox databases.
This script contains one advanced function, Get-OSCEXMailboxDatabaseStatistics. You can use this script in following ways:
Method 1:
Method 2:
Function Get-OSCEXMailboxDatabaseStatistics
{
[CmdletBinding()]
Param
(
#Define parameters
[Parameter(Mandatory=$true,Position=1)]
[string[]]$MailboxProperty,
[Parameter(Mandatory=$true,Position=2)]
[string[]]$MailboxDatabaseProperty,
[Parameter(Mandatory=$false,Position=3)]
[string]$MailboxFilter
)
Process
{
#Define two variables for storing the information.
$results = @()
$mbxDBStatistics = @{}
#Define a ProgressRecord
$activityName = $Messages.ActivityName
$progressRecord = New-Object System.Management.Automation.ProgressRecord(1,$activityName,"Processing")
#Try to get mailbox servers
if (-not ([System.String]::IsNullOrEmpty($MailboxFilter))) {
$mailboxes = Get-Mailbox -Filter $MailboxFilter -ResultSize unlimited -Verbose:$false
} else {
$mailboxes = Get-Mailbox -ResultSize unlimited -Verbose:$false
}
if ($mailboxes -ne $null) {
foreach ($mailbox in $mailboxes) {
$counter++
if ($mailboxes -is [array]) {
$progressPercent = [int]($counter / ($mailboxes.Count) * 100)
}
#Define a variable for storing the information.
$result = New-Object PSObject
#Display progress
$verboseMsg = $Messages.ProcessingMailbox
$verboseMsg = $verboseMsg -replace "Placeholder01", $($mailbox.Alias)
$pscmdlet.WriteVerbose($verboseMsg)
$progressRecord.CurrentOperation = $verboseMsg
$progressRecord.PercentComplete = $progressPercent
$pscmdlet.WriteProgress($progressRecord)
#Begin to process
$mbxServerName = $mailbox.ServerName
$mbxDBName = $mailbox.Database
$result | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $mbxServerName
$result | Add-Member -MemberType NoteProperty -Name "Database" -Value $mbxDBName
if (-not ($mbxDBStatistics.ContainsKey($mbxDBName))) {
$mbxDB = Get-MailboxDatabase -Identity $mbxDBName -Status -Verbose:$false
$mbxDBStatistics.Add($mbxDBName,$mbxDB)
}
foreach ($mbxDBProperty in $MailboxDatabaseProperty) {
$mbxDBPropertyValue = $mbxDBStatistics[$mbxDBName].$mbxDBProperty
$result | Add-Member -MemberType NoteProperty -Name "$mbxDBProperty" -Value $mbxDBPropertyValue
}
foreach ($mbxProperty in $MailboxProperty) {
$result | Add-Member -MemberType NoteProperty -Name "$mbxProperty" -Value $($mailbox.$mbxProperty)
}
$results += $result
}
} else {
#Cannot find mailboxes with specified filter
$errorMsg = $Messages.CannotFindMBXWithSpecifiedFilter
$errorMsg = $errorMsg -replace "Placeholder01",$MailboxFilter
$customError = New-OSCPSCustomErrorRecord `
-ExceptionString $errorMsg `
-ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet
$pscmdlet.WriteError($customError)
return $null
}
#Return the results
return $results
}
}
Function Get-OSCEXMailboxDatabaseStatistics { [CmdletBinding()] Param ( #Define parameters [Parameter(Mandatory=$true,Position=1)] [string[]]$MailboxProperty, [Parameter(Mandatory=$true,Position=2)] [string[]]$MailboxDatabaseProperty, [Parameter(Mandatory=$false,Position=3)] [string]$MailboxFilter ) Process { #Define two variables for storing the information. $results = @() $mbxDBStatistics = @{} #Define a ProgressRecord $activityName = $Messages.ActivityName $progressRecord = New-Object System.Management.Automation.ProgressRecord(1,$activityName,"Processing") #Try to get mailbox servers if (-not ([System.String]::IsNullOrEmpty($MailboxFilter))) { $mailboxes = Get-Mailbox -Filter $MailboxFilter -ResultSize unlimited -Verbose:$false } else { $mailboxes = Get-Mailbox -ResultSize unlimited -Verbose:$false } if ($mailboxes -ne $null) { foreach ($mailbox in $mailboxes) { $counter++ if ($mailboxes -is [array]) { $progressPercent = [int]($counter / ($mailboxes.Count) * 100) } #Define a variable for storing the information. $result = New-Object PSObject #Display progress $verboseMsg = $Messages.ProcessingMailbox $verboseMsg = $verboseMsg -replace "Placeholder01", $($mailbox.Alias) $pscmdlet.WriteVerbose($verboseMsg) $progressRecord.CurrentOperation = $verboseMsg $progressRecord.PercentComplete = $progressPercent $pscmdlet.WriteProgress($progressRecord) #Begin to process $mbxServerName = $mailbox.ServerName $mbxDBName = $mailbox.Database $result | Add-Member -MemberType NoteProperty -Name "ServerName" -Value $mbxServerName $result | Add-Member -MemberType NoteProperty -Name "Database" -Value $mbxDBName if (-not ($mbxDBStatistics.ContainsKey($mbxDBName))) { $mbxDB = Get-MailboxDatabase -Identity $mbxDBName -Status -Verbose:$false $mbxDBStatistics.Add($mbxDBName,$mbxDB) } foreach ($mbxDBProperty in $MailboxDatabaseProperty) { $mbxDBPropertyValue = $mbxDBStatistics[$mbxDBName].$mbxDBProperty $result | Add-Member -MemberType NoteProperty -Name "$mbxDBProperty" -Value $mbxDBPropertyValue } foreach ($mbxProperty in $MailboxProperty) { $result | Add-Member -MemberType NoteProperty -Name "$mbxProperty" -Value $($mailbox.$mbxProperty) } $results += $result } } else { #Cannot find mailboxes with specified filter $errorMsg = $Messages.CannotFindMBXWithSpecifiedFilter $errorMsg = $errorMsg -replace "Placeholder01",$MailboxFilter $customError = New-OSCPSCustomErrorRecord ` -ExceptionString $errorMsg ` -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $pscmdlet $pscmdlet.WriteError($customError) return $null } #Return the results return $results } }
Example 01: Displays help about Get-OSCEXMailboxDatabaseStatistics
Command: Get-Help Get-OSCEXMailboxDatabaseStatistics -Full
Screenshot:

Example 02: Retrieve specified property values for all mailboxes
Command:
Get-OSCEXMailboxDatabaseStatistics -MailboxProperty "Name","Alias" -MailboxDatabaseProperty "LastFullBackup","DatabaseSize" | Format-Table -Autosize
Screenshot:

Example 03: Retrieve specified property values for these mailboxes which alias starts with "TestUser0"
Command:
Get-OSCEXMailboxDatabaseStatistics -MailboxProperty "Name","Alias" -MailboxDatabaseProperty "LastFullBackup","DatabaseSize" -MailboxFilter 'Alias -like "TestUser0*"' | Format-Table -Autosize
Screenshot:

Example 04:
Retrieve specified property values for these mailboxes which alias starts with "TestUser0" and exports the results to a comma-separated values (CSV) file.
Command:
Get-OSCEXMailboxDatabaseStatistics -MailboxProperty "Name","Alias" -MailboxDatabaseProperty "LastFullBackup","DatabaseSize" -MailboxFilter 'Alias -like "TestUser0*"' | Export-Csv -Path c:\Scripts\mailbox-statistics.csv -NoTypeInformation
Screenshot:

Note:
Please do not use wildcard character(*) in the property name or as the value of this parameter.
Parameters like -MailboxProperty "N*" , -MailboxDatabaseProperty "*backup*" and -MailboxProperty "*" are not qualified for Get-OSCEXMailboxDatabaseStatistics.
And the property names of MailboxProperty and MailboxDatabaseProperty should not be conflicted with each other. For example, the following command will result an error:
Get-OSCEXMailboxDatabaseStatistics -MailboxProperty "Name","Alias" -MailboxDatabaseProperty "Name","DatabaseSize" -MailboxFilter 'Alias -like "TestUser0*"'

Technical Resource:
Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx
Get-Mailbox
http://technet.microsoft.com/en-us/library/bb123685.aspx
Get-MailboxDatabase
http://technet.microsoft.com/en-us/library/bb124924.aspx
Forum Threads:
Script required to count mailbox per database
http://social.technet.microsoft.com/Forums/da-DK/exchange2010/thread/b470fe9c-054a-45cc-8901-9203a81cca01
Powershell command to count the number of mailboxes per database in Exchange 2010
http://social.technet.microsoft.com/Forums/pl-PL/exchange2010/thread/99dab102-1b49-4ce2-8b85-dc247ae1fc84
Number of mailboxes per database
http://social.technet.microsoft.com/Forums/en-US/exchangesvrgeneral/thread/eeb49d0f-3d07-4baf-8d55-17a9de9f7398