Gary's $this and that about PowerShell

I am a Microsoft Senior Premier Field Engineer based out of Atlanta, GA. My focus is Powershell. This blog is mainly to share interesting Powershell script samples.

List Mailbox Sizes for both Exchange 2003 and Exchange 2007

List Mailbox Sizes for both Exchange 2003 and Exchange 2007

  • Comments 12
  • Likes

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 mailboxsizeinMBDescending     
        
            
#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.

Comments
  • your code is formatted really poorly.

  • Wow, it only took over 4 years to get my first comment on this post, and its a nice negative one.  Sometimes in life its great to bring proposed solutions along side the problems.  In this case though I went ahead and took a guess that you didn't like the extra line feeds in the code.  I never liked them either, but at the time so long ago that I wrote this code, I had a heck of a time getting colored code into these blog posts nicely.  This post drove me nuts enough that I gave up.  Thankfully, years later, now its gotten much easier.  I have removed all those wasteful line feeds.  I hope this is what you were thinking about when posted your comment anon.  If you want it to look another way, please do tell how you would like to see it.

    Also, wow, you still have Ex 2000/2003 around.

  • 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

  • Hi Gary,

    I thought I would follow up on Nick's comment and say how helpful this script is. Thanks for posting it.

    JT

  • 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 ?

  • Lol. It seems this is more popular now. Id guess thats because Ex2003 is nearing end of life.

  • Thanks Gary, weirdly enough this is *exactly* what I was looking for :)

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

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

  • 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.

  • worked as espected. Thanks

  • Thank Gary,
    Great script. It was a real big help.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment