Share via


List Mailbox Sizes for both Exchange 2003 and Exchange 2007

This is a script I put together because…well I don't remember what prompted me to write it.  Who cares :)

This script uses the built-in cmdlets for Exchange 2007 find 2007 servers and legacy servers.  It then uses WMI to talk to the legacy servers to get the mailbox sizes.  Technically, this script requires Exchange 2007 Management tools to be installed to even get this kicked off.  It wouldn't be very difficult to have it check for the existence or registration of the Exchange snap-in and behave differently if it were run on an a non Exchange 2007 server (or tools machine).  It would require PowerShell to be installed.  Moral of the story, it should be able to made to work against just Exchange 2000 and 2003 without 2007.

I am going to confess, I haven't been able to test this extensively.  If anyone out there runs into issues with it, let me know and I can get it fixed up.  I asked a few colleagues, but they all are bums and didn't test it for me. :)

Let me know what you think either way.

Here is the sample code:

 # Script to get mailbox sizes from both Exchange 2007+ servers and legacy Exchange             # 2000 and 2003 Servers             #             # This script is provided "AS IS" with no warranties, and confers no rights.             # Use of included script samples are subject to the terms specified at             # https://www.microsoft.com/info/cpyright.htm.             # Written by: Gary Siepser, Microsoft, Premier Field Engineer
                 #Get list of only E2k7 or later mailboxes as we get their size through a cmdlet             #, but the mailboxes still on E2K3 have to come from WMI             $2007Users = Get-Mailbox -RecipientTypeDetails usermailbox   
                       #Get a list of the legacy servers so we can connect to WMI on each one             $legacyservers = get-exchangeserver | Where-Object {-not $_.IsExchange2007OrLater}  
                       #get the stats on all the E2K7 Mailboxes.  This may yield a number of red and yellow             #errors and warnings respectively.  These can be squashed through error and warning             #preference.             $2007Userstats = $2007Users | get-mailboxstatistics
                 #loop through the legacy servers to get the userstats through WMI             foreach ($server in $legacyservers)             {                 # Use the right WMI namespace and class to get to mailbox data and add it to a building                 # array to hold it all so we can combine this with the non-legacy user data already                 # collected                 $legacyusers += Get-WmiObject -ComputerName $server -Namespace root\MicrosoftExchangeV2 -Class Exchange_mailbox             }    
                      #Now we have all the data in two seperate variables.  Unfortunately the objects types             # in those variables is quite different. We are going to go ahead and create a simple             # new object type and populate it with the data from each list to make a new common             # list of mailbox names and sizes.             #Create the object and give it the properties we want to use             $templateobject = New-Object PSObject             $templateobject = $templateobject | Select-Object Name,Server,MailboxSizeinMB
               # Now we need to loop through the users lists and populate these custom objects             # into a new array
              #Create the empty array             $results = @()  
                      # Loop through the 2007 user stats first             foreach ($user in $2007Userstats)             {                 #copy the template object to this actual instance we want to add to the building list                 $newobjectInstance = $templateobject |  Select-Object *      
                        #Populate the properties with the data                 $newobjectInstance.Name = $user.DisplayName                 $newobjectInstance.Server = $user.ServerName   
                           #We need to pull out the size in Bytes from the object that is in totalitemsize                 # and then devide it by 1MB and round it off.  This gives us a nice number with                 # two decimals places...nicer that what the .ToMB() method gives us)                 $newobjectInstance.MailboxSizeinMB = [Math]::Round(($user.totalitemsize.value.ToBytes() / 1MB),2)      
                        #add this object isntance to the result array                 $results += $newobjectInstance             }        
                  #now loop through the WMI data we got back for the legacy servers and users             foreach ($user in $legacyusers)             {                 #copy the template object to this actual instance we want to add to the building list                 $newobjectInstance = $templateobject |  Select-Object *       
                       #Populate the properties with the data                 $newobjectInstance.Name = $user.MailboxDisplayName                 $newobjectInstance.Server = $user.ServerName       
                       #We need to pull out the size in Bytes from the object that is in totalitemsize                 # and then devide it by 1MB and round it off.  This gives us a nice number with                 # two decimals places...nicer that what the .ToMB() method gives us)                 $newobjectInstance.MailboxSizeinMB = [Math]::Round(($user.size / 1MB *1KB),2)            
                  #add this object isntance to the result array                 $results += $newobjectInstance             }            
              # Sort the results in a more meaningful way            $results = $results | Sort-Object mailboxsizeinMB –Descending     
                     #Do whatever you want now, in this case simply output the results.            $results                                             

-Gary

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at https://www.microsoft.com/info/cpyright.htm.

Comments

  • Anonymous
    January 01, 2003
    I love that four and a half years later this post keeps on giving. Glad its helping you folks.

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    April 29, 2013
    your code is formatted really poorly.

  • Anonymous
    May 01, 2013
    Gary, I just thought I'd let you know this code helped me craft a move mailbox strategy capturing the small mailboxes first and moving the legacy Exchange 2003 boxes to 2010.  This was helpful code.   Thanks, Nick

  • Anonymous
    August 19, 2013
    Hi Gary, I thought I would follow up on Nick's comment and say how helpful this script is. Thanks for posting it. JT

  • Anonymous
    August 29, 2013
    Thanks Gary.. It helped me formulate a strategy for a migration project. I would though like to to have "StorageLimitInfo" included in the final result for every user . Can you please guide how to ?

  • Anonymous
    October 24, 2013
    Lol. It seems this is more popular now. Id guess thats because Ex2003 is nearing end of life.

  • Anonymous
    January 16, 2014
    Thanks Gary, weirdly enough this is exactly what I was looking for :)

  • Anonymous
    January 29, 2014
    Thanks a lot Garry, its very useful and so simple to understand tahnk you once again

  • Anonymous
    June 18, 2014
    Thanks. Works well with Exchange 2010 and 2003.
    I've added it to a script puts the data in boxes, adds a bit of colour and emails it as a weekly report.
    Nice work.

  • Anonymous
    July 18, 2014
    worked as espected. Thanks

  • Anonymous
    July 27, 2014
    Thank Gary,
    Great script. It was a real big help.

  • Anonymous
    March 11, 2015
    We STILL have customers that are using Exchange Server 2003. Thanks very much for the script; it really helped with the migration.