Remove-DisconnectedMailbox

Author by Matt Sims

In certain circumstances, it's useful to forcefully remove disconnected mailboxes from Exchange Server 2010 that have been either disabled or soft deleted.  With service pack 1 for Exchange 2010, Microsoft introduces this capability with the Remove-StoreMailbox cmdlet.  Below is a small script (to be executed from an Exchange Management Shell prompt) that will get all the disconnected mailbox objects that are disabled or soft deleted in a given database and remove them.
#RUN FROM THE EXCHANGE 2010 SP1/SP2 MANAGEMENT SHELL
#CREATE DATABASE VARIABLE AND ACCEPT USER INPUT
$Database=Read-Host "Please Enter A Database Name (i.e. DB01)"
#GET ALL MAILBOXSTATISTICS OBJECTS FROM SPECIFIED SERVER WHERE THE MAILBOX IS DISABLED
Get-MailboxStatistics -Database $Database | Where-Object {$_.DisconnectReason -eq 'Disabled'} |
#EXECUTES FOREACH-OBJECT LOOP WHICH REMOVES DISCONNECTED AND DISABLED MAILBOXES FROM EACH DATABASE ON SPECIFIED SERVER
ForEach-Object {
    Remove-StoreMailbox -Database $_.Database -Identity $_.MailboxGuid -MailboxState Disabled
    }
#GET ALL MAILBOXSTATISTICS OBJECTS FROM SPECIFIED SERVER WHERE THE MAILBOX IS SOFT DELETED
Get-MailboxStatistics -Database $Database | Where-Object {$_.DisconnectReason -eq 'SoftDeleted'} |
#EXECUTES FOREACH-OBJECT LOOP WHICH REMOVES DISCONNECTED AND SOFT DELETED MAILBOXES FROM EACH DATABASE ON SPECIFIED SERVER
ForEach-Object {
    Remove-StoreMailbox -Database $_.Database -Identity $_.MailboxGuid -MailboxState SoftDeleted
    }
 
Author

Matt Sims

Solution Lead - Unified Communications