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

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filena

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is. I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned) #Define Date to Query $StartDate = (Get-Date) $EndDate

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.