Powershell Exchange 2007 Mailbox Usage Report

I recently had a need to run a scheduled report on an Exchange 2007 server to see what the current mailbox usage was.

 

The problem was that I needed to make the report available to the executives, and I surely didn't want to give them access to any the Exchange management tools. I especially didn't want to have to run the report myself every time someone got the wild idea that they needed to audit the mailbox usage for the company, either.

 

To solve this, I wrote a simple script that generated a report and dumped it to an HTML file on a schedule. Then, the execs could view the report at their leisure.

 

Here are the gritty details:

 

# Start
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

# Change this next line to something like:
# $data = Get-Content 'header.html';
# If you want to have an external header file
$data = "<html><head><title>Mailbox Usage Report</title></head>";

$data += "<table>";
$data += "<tr><td>Name</td><td>EmailAddress</td><td>ItemCount</td><td>ItemSizeMB</td></tr>"
Get-Mailbox | foreach {
$name = $_.DisplayName.ToString();
$email = $_.PrimarySmtpAddress.ToString();
$tmp = $_ | get-mailboxstatistics;
$items = $tmp.ItemCount.ToString();
$size = $tmp.TotalItemSize.value.ToMB();
$data += "<tr><td>$name</td><td>$email</td><td>$items</td><td>$size</td></tr>";
}

$data += "</table>";
$t = get-date;
$data += "<br />Generated: " + $t.ToString() + "<br />";

# Change this next line to something like:
# $data += Get-Content 'footer.html';
# If you want to have an external footer file
$data += "</body></html>";

# Now dump this onto the webserver somewhere so people can view it
echo $data | out-file -filePath 'c:\inetpub\wwwroot\reports\mailbox_sizes.html';

# Finished

That's it! Now you just need to save this to a powershell file somewhere and you can either run it manually, or set up a scheduled task to run it.

 



Back to Article Index

blog comments powered by Disqus