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

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...

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/filen...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
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.