Exchange_2010_logo_www.oostdam.infoExchange Server 2007 & 2010 are really beautifull products from Microsoft. Ofcourse these products have difficulities of there own, but generally, there functionality is great. With the integration of the Exchange Powershell command line tools, the products took a great step forward, allowing (exchange)administrators lots of (scripting) possibilities and remote control of servers. This serie of articles has been made to share knowledge and provide some basic default scripting, which can be used on a (daily) basis through Exchange Powershell.  It can be an extra value for every organisation as "knowing" and "numbers" can be used to discover (data)trends, excessions and as a basis for decisions and policies. Allthough the default scripting will be done in Powershell for Exchange 2010, I will try to add some explanation and syntax for Exchange 2007, or at least the differences, on the side. Have fun using powershell for Exchange !

List mailboxsizes and items with an exchange powershell script:
This script has been made to export the mailbox sizes in MB and the item count for every mailbox existing on any database store on a exchange server. For all my script editing I usually use Notepad++ by the way. The latest version you can can download at this url. But now first for the basic Exchange 2010 Powershell command line. 

Get-MailboxStatistics -Server SERVERHOSTNAME | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount > C:\Scripts\ExportMailboxSize.txt

Did you see the result?
Next question will be in which maildatabase the mailbox actually resides, so we add the database name (in the color blue)

Get-MailboxStatistics -Server SERVERHOSTNAME | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount,Database >

OK, that is a nice result. But now that we now in which database a users mailbox resides?
How about an extra column to see if a user received a mailbox quota warning? So we add the Status (in the color blue)

Get-MailboxStatistics -Server SERVERHOSTNAME | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount,Database,StorageLimitStatus > C:\Scripts\ExportMailboxSize.txt

The Powershell command for 2007 is a little different:
Get-MailboxStatistics | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount > C:\Scripts\ExportMailboxSize.txt

I guess you can figure out the various commands to use in Exchange 2007 yourself!!

Replace the red colored text for the host/FQDN name of your exchange server and adjust the drive, directory or scriptname to your own preferences.

Creating this for use in a scheduled powershell ps1 script needs a little extra command at front.
You can copy/paste the following text and save it as a PS1 file with the name of your choice. Call it exportmailboxsize.ps1 or something like that.  
- - - - - - - - - - - - - - - - - - -
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
Get-MailboxStatistics -Server SERVERNAME | Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(KB)";expression={$_.TotalItemSize.Value.ToKB()}},ItemCount > C:\Scripts\ExportMailboxsize.txt
- - - - - - - - - - - - - - - - - - -

Rename the earlier mentioned variables to your environment.

For use in exchange 2007 rename the E2010 at the end of the first line to admin. That will do the trick.
icrosoft did changed the syntax a little bit, perhaps to keep us administrators busy I don't know.

Possible signing errors:
You have to tell Exchange 2010 and 2007 that they can trust other ps1 scripts then the default set that comes along with the installation. This is a one time command, after that Powershell for Exchange will trust and execute custom made scripts. The command is: Set-ExecutionPolicy RemoteSigned Please be aware of that. I often run this command manually first en set it back after I'm done, but everyone should make these security decisions for there own.

Remote availability & what to do with the report:
You can run this powershell command from, and towards, another Exchange server, if you have sufficiënt administrative rights. You can copy/paste the results to a shared directory to your customer, or make it available for your internal organisation on a sharepoint/intranet site if you have one available. Please do not share folders for "everyone" and always dubble check the permissions granted. Some reports can contain delicate information and you do not want that, ever, to get internally, or worse, publicly, available. Again, please pay extra attention to this. Creating a scheduled task and mailing the reports to a person might be a nice work-around. See the next lines on how to create a scheduled task.

Create a scheduled task in Windows 2008(R2):
When you are using Windows 2008 server, this is very easy. Create a scheduled basic task and choose for the email option. The rest shows itselfs almost. A little extra attention for the re-run, wake-on-lan, and retry options will be necessary. Also, by default, the report/mail will not run until the user who created the task is loggend on. To solve that, please check the radio-button "Run whether user is logged on or not" on the General Tab, and it will always run on the scheduled time. Please always use a special service account for running and scheduling reports of this kind. When it is compromised, you can rather easiily change it. Do not, ever, use your own (admin)account or administrator account to schedule reports or mails.

I probally do not have to remind you, that you should always use scripts with care.
Therefore this posting is "as is" without express or implied warranty.
I would be nice though, if you put this line # Created by - Ben Oostdam  in the first line of your PS1 file.
Just for my efforts to create this article, which you must like if you've read it this far. 

Printscreen: not available

Ben OostdamBen Oostdam has been working with Windows systems since 1993. Worked for several companies as a system administrator, and is currently a Senior Support Engineer for a large company in the Netherlands.

Disclaimer: The information contained in this website/article is for general information purposes only. The information is provided as is, by Ben Oostdam and others, and while we endeavour to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the website for any purpose. Any reliance you place on such information is therefore strictly at your own risk. In no event will we be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website. Through this website you are sometimes able to link to other websites which are not under my control. I have no control over the nature, content and availability of those sites. The inclusion of any links does not necessarily imply a recommendation or endorse the views expressed within them. Every effort is made to keep the website up and running smoothly. However, I take no responsibility for, and will not be liable for, the website being temporarily unavailable due to technical issues beyond our control. All entries in these articles, are my individual opinion, or from co-writers, and they don't necessary reflect the opinion of my employer.


Friday the 7th, May 2021. All rights reserved.. // Oostdam WebDesign