Home > Exchange 2007, Exchange 2010, Scripting > Exchange 2010 Mailbox Count, Database Size, and Average to HTML

Exchange 2010 Mailbox Count, Database Size, and Average to HTML

I decided to work on an Exchange 2010 PowerShell, HTML Status page script that would dump the following Exchange 2010 Mailbox information statistics:

  1. Exchange Mailbox and Distribution List counts per domain
  2. Total Exchange Mailbox Count, with averages and totals of Mailbox size and content
  3. Total number of Databases with raw storage used, averages, and max and min DB sizes
      The script:  Save the script below as

c:\scripts\Mailbox-Count-HTML-New.ps1.

#Author: Ed McKinzie edward@i-tekpro.com
# Site: www.i-tekpro.com
#Exchange 2010 Microsoft Engineer

#Delete any previous copies of the HTML page
del C:\Scripts\Exchange-Mailbox-Stats\Exchange-MailboxStats-$filedate.html

#Date the file
$filedate = (get-date).tostring("MM-dd-yyyy")

#Create the file

$file = New-Item -type file "C:\Scripts\Exchange-Mailbox-Stats\Exchange-MailboxStats-$filedate.html"

#File creation status
Write-Output $file

#Ignore any errors
$erroractionpreference = "SilentlyContinue"
$warningactionpreference = "SilentlyContinue"

#Add content to the HTML Page, including Body and Headers
Add-Content $file "<HTML><TITLE>Exchange Mailbox Information</TITLE>"
Add-Content $file "<HEAD><H1 class='auto-style1'><CENTER>Exchange Mailbox Information</Head></CENTER></H1>"
#Add-Content $file "<BR><HR class='auto-style1'>"
Add-Content $file "<Body bgcolor='#000000'>"
#####################################################################################################################
#####################################################################################################################
#####################################################################################################################

#Set the Domain
Set-AdServerSettings -RecipientViewRoot "vlab.com"
write-host "Counting Vlab Domain Mailboxes....."
$MailboxCount1 = (Get-Recipient -resultsize unlimited -RecipientType 'UserMailbox') | Measure-Object | Select Count

#####################################################################################################################
#####################################################################################################################
#####################################################################################################################
$DLCount1 = (Get-Recipient -ResultSize Unlimited -RecipientType 'DynamicDistributionGroup','MailNonUniversalGroup','MailUniversalDistributionGroup','MailUniversalSecurityGroup')| Measure-Object | Select Count

#####################################################################################################################
###DOMAIN #2 Uncomment this section and change the domain if you have a multi-Domain AD Forest
#Set-AdServerSettings -RecipientViewRoot "Contoso.com"
#write-host "Counting Contoso.com mailboxes Mailboxes....."
#$MailboxCount2 = (Get-Recipient -resultsize unlimited -RecipientType 'UserMailbox') | Measure-Object | Select Count
#$DLCount2 = (Get-Recipient -ResultSize Unlimited -RecipientType 'DynamicDistributionGroup','MailNonUniversalGroup','MailUniversalDistributionGroup','MailUniversalSecurityGroup')| Measure-Object | Select Count
#####################################################################################################################

#####################################################################################################################
#$MailboxSizeDump = Get-Mailbox -Resultsize unlimited | Get-MailboxStatistics | %{$_.TotalItemSize.Value.ToMB()} | Measure-Object -sum -average -max -min
# Retrieve the list of mailboxes from all mailbox databases on all servers

$listofmailboxdatabases = get-mailboxdatabase
$totalmailboxcount = 0
$totalmailboxtotalitemcount = 0
$totalmailboxtotalsize = 0

# start a loop that will check each mailbox database
foreach ($individualdatabase in $listofmailboxdatabases)
{

$listOfMailboxes = Get-MailboxDatabase $individualdatabase | Get-Mailbox

# Initialize the counter variables that we'll use
# Initialize the counter variables that we'll use
$mailboxCount = 0
$mailboxTotalItemCount = 0
$mailboxTotalSize = 0
$mailboxAverageSize = 0
$mailboxAverageItemCount = 0

# Start a loop that will count stats from individual mailboxes
foreach ($individualMailbox in $listOfMailboxes)
{
# increment the mailbox count by 1
$mailboxCount++

# Get the name of the current mailbox so that we can...
$individualMailboxName = $individualMailbox.Identity.DistinguishedName

#... quickly and easily get stats from that mailbox
$individualMailboxStats = Get-MailboxStatistics -Identity $individualMailbox

# Get the size of the mailbox in MB and save it in a variable
$individualMailboxSize = $individualMailboxStats.TotalItemSize.value.toMB()

# Get the number of items in the mailbox and save it in a variable
$individualMailboxItemCount = $individualMailboxStats.ItemCount

# Add the size of this mailbox to a running total
$mailboxTotalSize = $mailboxTotalSize + $individualMailboxSize

# Add the number of items in this mailbox to a running total
$mailboxTotalItemCount = $mailboxTotalItemCount + $individualMailboxItemCount
}

# Calculate the average mailbox size
$mailboxAverageSize = $mailboxTotalSize / $mailboxCount

# Calculate the average number of items per mailbox
$mailboxAverageItemCount = $mailboxTotalItemCount / $mailboxCount

$totalmailboxcount = $totalmailboxcount + $mailboxCount
$totalmailboxtotalsize = $totalmailboxtotalsize + $mailboxTotalSize
$totalmailboxtotalitemcount = $totalmailboxtotalitemcount + $mailboxTotalItemCount
}

# Calculate the average mailbox size
$totalmailboxAverageSize = $totalmailboxtotalsize / $totalmailboxcount

# Calculate the average number of items per mailbox
$totalmailboxAverageItemCount = $totalmailboxtotalitemcount / $totalmailboxcount

####################################################################################################################

write-host "Measuring Database statistics....."
$MailboxStorageStats = get-mailboxdatabase | foreach-object{select-object -inputobject $_ -property *,@{name="MailboxDBSizeinGB";expression={[math]::Round(((get-item ("\\" + $_.servername + "\" + $_.edbfilepath.pathname.replace(":","$"))).length / 1GB),2)}}} | Sort-Object mailboxdbsizeinGB -Descending | measure-object -Property mailboxdbsizeinGB -Sum -Maximum -Minimum -Average

#####################################################################################################################

$MBXCount = "
{0:n0}" -f ($mailboxcount1.Count)
$DLCount = "
{0:n0}" -f ($DLCount1.Count)

#If you have multiple domains, enable this to coincide with Domain#2
#$MBXCount2 = "
{0:n0}" -f ($mailboxcount2.Count)
#$DLCount2 = "
{0:n0}" -f ($DLCount2.Count)


$MBXDatabaseCount = "
{0:n0}" -f ($MailboxStorageStats.Count)
$MBXDatabaseSizeTotal = "
{0:n2}" -f ($MailboxStorageStats.Sum)
$MBXDatabaseAverage = "
{0:n2}" -f ($MailboxStorageStats.Average)
$MBXDatabaseLargest = "
{0:n2}" -f ($MailboxStorageStats.Maximum)
$MBXDatabaseSmallest = "
{0:n2}" -f ($MailboxStorageStats.Minimum)


$MBXCount_Total = "
{0:n0}" -f ($totalmailboxcount)
$MBXTotalItemSize = "
{0:n0}" -f ($totalmailboxtotalitemcount)

$MBXSizeTotal = "
{0:n2}" -f ($totalmailboxtotalsize/1000)
$MBXAverage = "
{0:n2}" -f ($totalmailboxAverageSize)
$MBXItemAverage = "
{0:n2}" -f ($totalmailboxAverageItemCount)


Add-content $file "
<h2 class='auto-style1'>Exchange Recipient Count</h2>"
Add-Content $file "
<style>table{border-style:solid;border-width:.5px;font-size:8pt;background-color:#ccc;width:80%;}th{text-align:left;}td{background-color:#000000;border-style:solid;border-width:.5px;}body{font-family:verdana;font-size:8pt;}h1{font-size:12pt;}h2{font-size:10pt;}.auto-style1 {color: #FFFFFF;}.auto-style2 { color: #000000;}</style>"
Add-content $file "<body bgcolor='#000000'><table class='auto-style1' BORDER='1'><TR class='auto-style2'><th>Domain</th><th>Mailbox\Recipient Count</th><th>Distribution List Count</th></tr>"
Add-content $file "<TR><TD>VLAB Domain","<TD>$MBXCount","<TD>$DLCount"

#Enable this for Domain #2....be sure to change the Domain#2 Name to the Domain Name Netbios name will suffice
#Add-content $file "<TR><TD>Contoso","<TD>$MBXCount2","<TD>$DLCount2"

Add-content $file "</Table>"
Add-Content $file "<BR>"

Add-content $file "<h1 class='auto-style1'>Exchange Total Mailbox Count</h1>"
Add-content $file "<table class='auto-style1' BORDER='1'><tr class='auto-style2'><th># Mailboxes</th><th>Total Mailbox Size</th><th>Total Items in Mailboxes</th><th>Average Mailbox Size</th><th>Average Items Per Mailbox</th></tr>"
Add-content $file "<TR><TD>$MBXCount_Total","<TD>$MBXSizeTotal GB","<TD>$MBXTotalItemSize</td>","<TD>$MBXAverage MB</td>","<TD>$MBXItemAverage</td>"
Add-content $file "</Table>"
Add-Content $file "<BR>"


Add-content $file "<h1 class='auto-style1'>Exchange Database Statistics</h1>"
Add-content $file "<table class='auto-style1' BORDER='1'><tr class='auto-style2'><th>Number of Databases</th><th>Total DB Size</th><th>Average DB Size</th></th><th>Largest DB Size</th><th>Smallest DB Size</th></tr>"
Add-content $file "<TD>$MBXDatabaseCount</TD>","<TD>$MBXDatabaseSizeTotal GB</TD>","<TD>$MBXDatabaseAverage GB","<TD>$MBXDatabaseLargest GB","<TD>$MBXDatabaseSmallest GB"
Add-content $file "</Table>"

#Add-Content $file "<BR><HR class='auto-style1'>"

$Datevar = (get-date).tostring("h:mm tt on M-dd-yy ")

Add-Content $file "<font color=#00FF00 font size='1'>This report starts Daily at 5 AM and finished at $Datevar</font>"

type $file

####################################################################################################################
#Change the TO and FROM variables and the SMTP server. Unless you allow anonymous SMPT sending, it is best run this script on your HUB server.
Send-MailMessage -To "oneyw@vlab.com" -From "Oneyw@vlab.com" `
-Subject "Exchange 2010 Mailbox Statistics - $Date" -SmtpServer "ex-cas1.vlab.com" -BodyAsHtml -Attachments C:\Scripts\Exchange-Mailbox-Stats\Exchange-MailboxStats-$filedate.html

##Invoke-Expression C:\Scripts\Exchange-Mailbox-Stats\Exchange-MailboxStats-$filedate.html

The Output:  Nice and organized HTML Page

image

If you want to automate this and archive the HTML file each day, you can create a batch file and call the PowerShell script using a scheduled task.

cd \ 

cd c:\scripts\

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto;C:\Scripts\Mailbox-Count-HTML-New.ps1"

Del "C:\inetpub\wwwroot\WebServer\Exchange-MBX-Size\Exchange-MailboxStats.html"

Copy "C:\Scripts\MailboxStats\*.html" "C:\inetpub\wwwroot\WebServer\Exchange-MBX-Size"

Rename "C:\inetpub\wwwroot\WEBSERVER\Exchange-MBX-Size\*.html" "Exchange-MailboxStats.html"

Move "C:\Scripts\MailboxStats\*.html" "C:\Scripts\MailboxStats\MailboxStats-Archive"

Best of luck!

Ed McKinzie

Advertisements
  1. MrT
    October 10, 2012 at 2:51 PM

    Could you put up a version of the script which will list the stats for each database in addition to the summary. So we can keep an eye out on the mailbox distribution between databases?
    DBname – DB Size – MB Count – Total Item Count – Avg MB size – Largest MB size – Smallest MB Size (or something similiar)
    thanks

  2. Ken Byrd
    August 2, 2013 at 1:56 PM

    Does this include the Archive email folder?

    thanks

  3. melatoroca
    December 2, 2013 at 12:55 PM

    great job, for those like me that are not able to script, amaizing

  4. Thomas
    December 6, 2013 at 1:01 PM

    I am unable to get this to work. I think this is for sp1 and 2 of 2010, The commands are different for sp3 on 2010. DO you have a version that will work on that?

    • Thomas
      December 6, 2013 at 1:38 PM

      I figured it out. I had opened a regular powershell window instead of an exchange ps.

  5. Tajvia
    October 26, 2014 at 6:19 PM

    This script is amazing! Thanks for sharing!

  1. July 11, 2014 at 6:15 AM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: