Exchange PowerShell Script: Get Mailbox Count by Database

 

Here’s a PowerShell script that does a quick count of mailboxes on each database of a server, or all servers – depending if you specify the server name as a script parameter.

** Updated:  I made some improvements to the script, and have posted the updated code.  One notable items is that I separated the count of mailboxes and disconnected mailboxes.  Previously, the count included both mailboxes and disconnected mailboxes.  This may skew the numbers a bit, depending on what  you are looking for.

   1: #=================================================================================================

   2: # NAME: Get-MailboxCountPerDatabase.ps1

   3: #

   4: # AUTHOR:  Dan B.

   5: # EMAIL:   Nerd@EverydayNerd.com 

   6: # DATE:    03/27/2011

   7: # Version: 1.0

   8: #

   9: # COMMENT: Script to return the count of mailboxes on each server by database

  10: #

  11: #=================================== Change Log ==================================================

  12: # Version 1.0

  13: # -Initial script

  14: # -Changed output to an array instead of just write-host

  15: # -Added total count to single server

  16: #

  17: #=================================================================================================

  18: # Script Paramaters to allow server name to be typed after the script name.   If no Param, all servers are returned

  19:  

  20: param([string] $Param )     

  21:  

  22: $Results = @()

  23: $CountMB = 0

  24:  

  25: if(!$param)

  26:     {

  27:         $Servers = Get-ExchangeServer | Where {$_.ServerRole -eq "Mailbox"} | Sort Name

  28:  

  29:         Foreach($Server in $Servers)

  30:             {

  31:                 $dbs = Get-MailboxDatabase -server $Server | Sort Name

  32:                 

  33:                 foreach($db in $dbs)

  34:                     {

  35:                         $mb = Get-MailboxStatistics -Database $db | Where {$_.DisconnectDate -eq $null -and $_.ObjectClass -eq 'Mailbox'} | Measure-Object

  36:                         $mbdis = Get-MailboxStatistics -Database $db | Where {$_.DisconnectDate -ne $null -and $_.ObjectClass -eq 'Mailbox'} | Measure-Object

  37:                         

  38:                         Write-Host "$($Server) `t $($db.name)`t $($a.count)"

  39:                         

  40:                         $Obj = New-Object PSObject

  41:                         $Obj | Add-Member NoteProperty -Name "Server" -Value $Server

  42:                         $Obj | Add-Member NoteProperty -Name "Database" -Value $db.Name

  43:                         $Obj | Add-Member NoteProperty -Name "Mailboxes" -Value $mb.count

  44:                         $Obj | Add-Member NoteProperty -Name "Disconnected Mailboxes" -Value $mbdis.count

  45:                         $Results += $Obj

  46:                     }

  47:             }

  48:     }

  49: else

  50:     {

  51:     $server = $param

  52:         $dbs = Get-MailboxDatabase -server $Server | Sort Name

  53:  

  54:         foreach($db in $dbs)

  55:             {

  56:                 $mb = Get-MailboxStatistics -Database $db | Where {$_.DisconnectDate -eq $null -and $_.ObjectClass -eq 'Mailbox'} | Measure-Object

  57:                 $mbdis = Get-MailboxStatistics -Database $db | Where {$_.DisconnectDate -ne $null -and $_.ObjectClass -eq 'Mailbox'} | Measure-Object

  58:  

  59:                 $Obj = New-Object PSObject

  60:                 $Obj | Add-Member NoteProperty -Name "Server" -Value $Server

  61:                 $Obj | Add-Member NoteProperty -Name "Database" -Value $db.Name

  62:                 $Obj | Add-Member NoteProperty -Name "Mailboxes" -Value $mb.count

  63:                 $Obj | Add-Member NoteProperty -Name "Disconnected Mailboxes" -Value $mbdis.count

  64:                 $Results += $Obj

  65:                 $countmb += $mb.count

  66:                 

  67:             }

  68:         Write-Host

  69:         Write-Host "$($Server) has a total of $($CountMB) mailboxes" -ForegroundColor Green

  70:     }

  71:  

  72: $Results | FT -AutoSize

  73:  

  74: #===================================== End of Script ==============================================

Script can be downloaded here:  Get-MailboxCountPerDatabase.ps1

Related posts:

  1. Script to Find Whitespace on all Exchange 2007 Servers
  2. Script: Get time of Exchange last online defrag
  3. Powershell Script: Display Exchange 2007 Queue by Site
  4. PowerShell: Drive Space Info from multiple systems
  5. Powershell Script to list all Cluster Active Nodes
This entry was posted in Exchange, PowerShell. Bookmark the permalink.

One Response to Exchange PowerShell Script: Get Mailbox Count by Database

  1. Peter says:

    Hi,
    The script works perfect, but it just counting the number of mailboxes on each database. Could be possible to show the mailboxes’ addresses (or primary SMTP address) on each database?!?
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>