Thursday, December 06, 2007

Mailbox Folder Size comparison Powershell GUI

While updating the mailbox size GUI the other week and getting a grip on the Get-Mailboxfolderstatitics cmdlet I had a few ideas of some other cool stuff you could do with this. For example if I used this cmdlet to get all the mailbox folders sizes on a server and then put them into a ADO.NET datatable I then have the ability to do a whole bunch of funky data manipulation that can be used to produce a whole bunch of useful information. For example I can show in a datagird a comparison between the sizes of everybody’s inbox, Sent Item, Deleted Item’s etc. Which allows me to answer questions like?

Who has the largest inbox on the server
Which users have too many items in their Inbox
How much each user has sitting in their deleted Items folder

One of the Properties Get-Mailboxfolderstatitics also allows you to see is the age of the newest item in the folder. So this can come in handy if you’re looking for mailboxes that might no longer be in use because you will be able to see and compare both what the date of newest item is in their inbox and sent items folder.

The script itself reuses a lot of the code from the mailbox size GUI the main difference is that it loops through ever mailbox on the server you select and runs the Get-Mailboxfolderstatitics on each mailbox. The results are then loaded into a ADO.NET datatable and then a dataview of this table is used with different Row Fitlers to filter out the particular folders you want to look at. To come up with the list of selected folders a hash table is used during the initial table fill and if the same folder name is found 5 times it’s added to the list of selectable folders. If this selectable folders list becomes a bit unmanageable you could tweak this value. The export code has been carried over from the mailboxsize gui so this allows you to export the results to a CSV file.

To cater for mailbox server with more the 1000 mailbox the -ResultSize Unlimited is used with the get-mailbox cmdlet. Also the –IncludeOldestAndNewestItems is used with Get-Mailboxfolderstatitics to ensure that the folder datetimes are included. This is something that was changed in SP1 so to cater for this I created two versions of the script a pre sp1 and post sp1 version.

Because the Get-Mailboxfolderstatitics cmdlet takes a while to run if you have a large number of mailboxes this script can take some time to process. It does write out its progress to the powershell cmd window if your worried the script may have hung. By default it will show a comparison of the inbox folder if your using a localized version of Exchange that has the inbox name in the local language then it will show you a blank screen but the folder list should get populated with your localized foldernames and the rest of the code should work fine.

I’ve put a download of the script here the SP1 version looks like

$form = new-object System.Windows.Forms.form
function processmailboxes(){
get-mailbox -Server $snServerNameDrop.SelectedItem.ToString() -ResultSize Unlimited | ForEach-Object{
$siSIDToSearch = get-user $_.DisplayName
write-host $_.DisplayName
Get-MailboxFolderStatistics $siSIDToSearch.SamAccountName.ToString() -IncludeOldestAndNewestItems | ForEach-Object{
$ficount = 0
$flastaccess = ""
$fisize = 0
$fsisize = 0
$fscount = 0
$fname = $_.Name
if ($fnhash.ContainsKey($_.Name)){
$fnhash[$_.Name] = [int]$fnhash[$_.Name] + 1
else {
if ($_.Name -ne $null){ $fnhash.add($_.Name,1)}
if ($_.FolderSize -ne $null){$fsisize = [math]::round(($_.FolderSize/1mb),2)}
if ($_.ItemsInFolder -ne $null){$ficount = $_.ItemsInFolder}
if ($_.NewestItemReceivedDate -ne $null){$flastaccess = $_.NewestItemReceivedDate}
if ($_.ItemsInFolderAndSubfolders -ne $null){$fscount = $_.ItemsInFolderAndSubfolders}
if ($_.FolderAndSubfolderSize -ne $null){$fsisize = [math]::round(($_.FolderAndSubfolderSize/1mb),2)}

$arylst = new-object System.Collections.ArrayList
$Dataveiw.RowFilter = "FolderName = 'Inbox'"
$dgDataGrid.DataSource = $Dataveiw
foreach ($key in $fnhash.keys){
if ($fnhash[$key] -gt 5) {
write-host $key
foreach ($val in $arylst){


function selectfolder(){
$Dataveiw.RowFilter = "FolderName = '" + $fnTypeDrop.SelectedItem.ToString() + "'"
$dgDataGrid.DataSource = $Dataveiw


function ExportFScsv{

$exFileName = new-object System.Windows.Forms.saveFileDialog
$exFileName.DefaultExt = "csv"
$exFileName.Filter = "csv files (*.csv)|*.csv"
$exFileName.InitialDirectory = "c:\temp"
if ($exFileName.FileName -ne ""){
$logfile = new-object IO.StreamWriter($exFileName.FileName,$true)
$logfile.WriteLine("SamAccountName,DisplayName,FolderName,# Items,Folder Size(MB),# Items + Sub,Folder Size + Sub(MB),Last Item Received")
foreach($row in $dgDataGrid.Rows){
if ($row.cells[0].Value -ne $null){
$logfile.WriteLine("`"" + $row.cells[0].Value.ToString() + "`",`"" + $row.cells[1].Value.ToString() + "`",`"" + $row.cells[2].Value.ToString() + "`"," + $row.cells[3].Value.ToString() + "," + $row.cells[4].Value.ToString() + "," + $row.cells[5].Value.ToString() + "," + $row.cells[6].Value.ToString() + "," + $row.cells[7].Value.ToString())

$fnhash = @{ }
$Dataset = New-Object System.Data.DataSet
$fsTable = New-Object System.Data.DataTable
$fsTable.TableName = "Folder Sizes"
$fsTable.Columns.Add("# Items",[int64])
$fsTable.Columns.Add("Folder Size(MB)",[int64])
$fsTable.Columns.Add("# Items + Sub",[int64])
$fsTable.Columns.Add("Folder Size + Sub(MB)",[int64])
$fsTable.Columns.Add("Last Item Received")
$Dataveiw = New-Object System.Data.DataView($fsTable)

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,20)
$snServerNamelableBox.size = new-object System.Drawing.Size(80,20)
$snServerNamelableBox.Text = "Server Name"

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(90,20)
$snServerNameDrop.Size = new-object System.Drawing.Size(100,30)
get-mailboxserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}

# folder Size Button

$fsizeButton = new-object System.Windows.Forms.Button
$fsizeButton.Location = new-object System.Drawing.Size(200,19)
$fsizeButton.Size = new-object System.Drawing.Size(120,23)
$fsizeButton.Text = "Get Folder Sizes"
$fsizeButton.visible = $True

# Add Folder Name Type Drop Down
$fnTypeDrop = new-object System.Windows.Forms.ComboBox
$fnTypeDrop.Location = new-object System.Drawing.Size(350,20)
$fnTypeDrop.Size = new-object System.Drawing.Size(150,30)
$fnTypeDrop.Add_SelectedValueChanged({if ($snServerNameDrop.SelectedItem -ne $null){SelectFolder}})

# Add Export FolderSize Button

$exButton1 = new-object System.Windows.Forms.Button
$exButton1.Location = new-object System.Drawing.Size(10,660)
$exButton1.Size = new-object System.Drawing.Size(125,20)
$exButton1.Text = "Export Folder Sizes"

# Add DataGrid View

$dgDataGrid = new-object
$dgDataGrid.Location = new-object System.Drawing.Size(10,50)
$dgDataGrid.size = new-object System.Drawing.Size(900,600)

$form.Text = "Exchange 2007 Mailbox Folder Compare Folder Size Form"
$form.size = new-object System.Drawing.Size(1000,620)
$form.autoscroll = $true
$form.topmost = $true