Determining free space in Exchange Mailbox Databases using PowerShell

Exchange Mailbox Databases (EDB files) increase in size automatically when required, but they never decrease in size without administrator intervention. Exchange does its best to use up the free space inside the database, the so called white space, but that is not always possible. Because of this you are left with database files that just continue to increase in size as data is added to them, even if a fair amount of free space is available inside the database file. So sometimes you have to take the database offline and manually defragment it using eseutil.exe. By doing this your databases will be defragmented and a new file created which does not have any white space in it. This new file will be automatically moved to the location of your old file that will be deleted. So how do you determine when it is time to defragment your databases, and how do you know how much your files will decrease in size?

The first question is up to you to decide but the second can be easily answered by a PowerShell cmdlet:

Get-EventLog -LogName Application -New 1024 | where { $_.EventID -eq 1221 -and $_.TimeGenerated -ge [DateTime]::Now.AddHours(-24) } | ft TimeGenerated,Message -Wrap -auto

I also have a script that does some formatting of the results, making it easier to export to CSV or XML e.g.

   1: $colResults = @()

   2: $events = Get-EventLog -LogName Application -New 1024 | where { $_.EventID -eq 1221 -and $_.TimeGenerated -ge [DateTime]::Now.AddHours(-24) }

   3: $events | ForEach `

   4: {

   5:  $objEvent = New-Object System.Object

   6:  $objEvent | Add-Member -type NoteProperty -name Date-Time -value $_.TimeGenerated

   7:  $objEvent | Add-Member -type NoteProperty -name Database -value $_.ReplacementStrings[1]

   8:  $objEvent | Add-Member -type NoteProperty -name "Free Space (MB)" -value $_.ReplacementStrings[0]

   9:  $colResults += $objEvent

  10: }

  11: $colResults | Sort-Object Database | ft -autosize

Happy defragmenting!

Leave a comment

Your email address will not be published. Required fields are marked *