Remove disconnected Exchange mailboxes using PowerShell

To totally unlock this section you need to Log-in

Following two functions, in Powershell, to manage disconnected (disabled or soft-deleted) mailboxes in Exchange 2010 (non-SP1, but it'll work even on SP1 and above).

The first function is called Get-DisconnectedMailbox and the name is pretty much self explanatory. This function will give you a list of all disconnected mailboxes on each of your mailbox servers. Take a look at the following code:

function Get-DisconnectedMailbox {

[Parameter(Position=0, Mandatory=$false)]
$Name = '*'
$mailboxes = Get-MailboxServer
$mailboxes | %{
$disconn = Get-Mailboxstatistics -Server $ | ?{ $_.DisconnectDate -ne $null }
$disconn | ?{$_.displayname -like $Name} |
Select DisplayName,

The code above use the DisconnectDate property to identify mailboxes that have been disconnected. The above function use a non-mandatory parameter to filter or select only certain users mailboxes.

Note: If you’ve recently deleted a mailbox, but it’s not showing up when running Get-DisconnectedMailbox, you may need to force Exchange to recognize this by running the Clean-MailboxDatabase cmdlet.

Running the function without specifying a user name will return all disconnected mailboxes:

Disconnected Exchange Mailboxes using PowerShell

Disconnected Exchange Mailboxes using PowerShell

To find a particular disconnected mailbox, just type the function name, followed by the users display name. For example, to determine the disconnected mailbox information for a user named Blanca Jacobs you could would run this command:

Get-DisconnectedMailbox “Blanca Jacobs”
Disconnected Exchange Mailboxes using PowerShell

Disconnected Exchange Mailboxes using PowerShell

The name parameter will accept wildcards. For example: Get-DisconnectedMailbox M* would give you all disconnected mailboxes starting with the letter “M”.

Purging Disconnected Mailboxes

You purge mailboxes using the Remove-Mailbox cmdlet, specifying the StoreMailboxIdentity and Database for the disconnected mailbox in question.

In an effort to simplify the purging of disconnected mailboxes, I wrote the Remove-DisconnectedMailbox function that is designed to work with Get-DisconnectedMailbox. Here is the code:

function Remove-DisconnectedMailbox {

[Parameter(Position=0, ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Parameter(Position=1, ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
process {
Remove-Mailbox @PSBoundParameters

This allows you to easily purge all disconnected mailboxes returned from Get-DisconnectedMailbox by piping the output to Remove-DisconnectedMailbox. You can also purge one disconnected mailbox at time, as shown here:

Get-DisconnectedMailbox “Bill Jones” | Remove-DisconnectedMailbox -Confirm:$false

Notice the use of -Confirm:$false in the above command. Since this is an advanced function, we can take advantage of ShouldProcess, which allows functions to use common cmdlet parameters such as -Confirm and -WhatIf.

If you look at the code closely, you’ll notice that this function is essentially a specialized wrapper for the Remove-Mailbox cmdlet. I use splatting with the $PSBoundParameters variable to automatically bind all of the function parameter values to Remove-Mailbox.

Connecting Disconnected Mailboxes

Of course, you can also use the Get-DisconnectedMailbox cmdlet in conjunction with the built-in Connect-Mailbox cmdlet to reconnect a mailbox to a user account.

For example, here’s how you would connect a disconnected mailbox for a user named Bradford Boyer:

Get-DisconnectedMailbox “Bradford Boyer” | %{Connect-Mailbox -Identity $_.StoreMailboxIdentity -Database ‘DB1′ -User ‘contoso\bboyer’ -Alias ‘bboyer’}
Disconnected Exchange Mailboxes using PowerShell

Disconnected Exchange Mailboxes using PowerShell

As you can see in the above example, you provide the StoreMailboxIdentity, Database, User and Alias to the Connect-Mailbox cmdlet. If you’d like to do this manually, you can determine the StoreMailboxIdentity of the disconnected mailbox using the Get-MailboxStatistics cmdlet.