#
#
# NAME: Get-MDBMailboxCount.ps1
#
# AUTHOR: Jan Egil Ring
# EMAIL:
jan.egil.ring@powershell.no#
# COMMENT: Script to retrieve number of users per mailbox database within the Exchange-organization.
# System.DirectoryServices.DirectorySearcher are used to gather information rather than "Get-Mailbox -Database x"
# due to high performance benefits.
# The information are stored in the variable $MDBInfo which may be used for further processing (i.e. retrieve the
# database with the lowest/highest number of users and so on).
# $MDBInfo are output to a CSV-file saved in current user`s Documents-folder.
# Tested against
Exchange 2010 only.
#
# For more information, see the following blog-post:
#
http://blog.powershell.no/2010/11/21/retrieve-number-of-mailboxes-per-database-in-exchange-server-2010#
# You have a royalty-free right to use, modify, reproduce, and
# distribute this script file in any way you find useful, provided that
# you agree that the creator, owner above has no warranty, obligations,
# or liability for such use.
#
# VERSION HISTORY:
# 1.0 20.11.2010 - Initial release
#
#
#Define function for counting number of mailboxes per mailbox database
function Get-MDBMailboxCount ([string]$DN) {
$Searcher = New-Object System.DirectoryServices.DirectorySearcher
$Searcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://$(([system.directoryservices.activedirectory.domain]::GetCurrentDomain()).Name)")
$Searcher.Filter = "(&(objectClass=user)(homeMDB=$DN))"
$Searcher.PageSize = 10000
$Searcher.SearchScope = "Subtree"
($Searcher.FindAll()).Count
}
#Variable for writing progress information
$Count = 1
#Variables for working with mailbox databases
$MDBs = Get-MailboxDatabase
$MDBInfo = @()
foreach ($MDB in $MDBs) {
#Write progress information
Write-Progress -Activity "Gathering database info..." -Status "Current database: $($MDB.Name)" -Id 1 -PercentComplete (($Count/$MDBs.Count) * 100)
#Create a new object and add custom note properties for each database
$obj = New-Object -TypeName psobject
$obj | Add-Member -Name "Mailbox Database" -Value $MDB.Name -MemberType NoteProperty
$obj | Add-Member -Name "Number of users" -Value (Get-MDBMailboxCount -DN $MDB.DistinguishedName) -MemberType NoteProperty
#Add current database-object to the $MDBInfo-array
$MDBInfo += $obj
#Increase counter variable
$Count ++
}
#Export $MDBInfo-array to a CSV-file
$MDBInfo | Export-Csv -Path $HOME\Documents\MDBUserCount.csv -NoTypeInformation