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             # http://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 http://www.microsoft.com/info/cpyright.htm.