Skip to main content

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

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$form = new-object System.Windows.Forms.form
function processmailboxes(){
$fsTable.clear()
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)}
$fsTable.Rows.add($siSIDToSearch.SamAccountName.ToString(),$siSIDToSearch.DisplayName.ToString(),$fname,$ficount,$fsisize,$fscount,$fsisize,$flastaccess)

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

}

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"
$exFileName.ShowDialog()
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())
}
}
$logfile.Close()
}
}

$fnhash = @{ }
$Dataset = New-Object System.Data.DataSet
$fsTable = New-Object System.Data.DataTable
$fsTable.TableName = "Folder Sizes"
$fsTable.Columns.Add("SamAccountName")
$fsTable.Columns.Add("DisplayName")
$fsTable.Columns.Add("FolderName")
$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")
$Dataset.tables.add($fsTable)
$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"
$form.Controls.Add($snServerNamelableBox)

# 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)}
$form.Controls.Add($snServerNameDrop)

# 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
$fsizeButton.Add_Click({processmailboxes})
$form.Controls.Add($fsizeButton)

# 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}})
$form.Controls.Add($fnTypeDrop)

# 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"
$exButton1.Add_Click({ExportFScsv})
$form.Controls.Add($exButton1)


# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,50)
$dgDataGrid.size = new-object System.Drawing.Size(900,600)
$form.Controls.Add($dgDataGrid)

$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
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.