• Home
  • Managing Disconnected Exchange Mailboxes with Powershell

Managing Disconnected Exchange Mailboxes with Powershell

Having worked with Exchange 2010/2013 and PowerShell for some time now, I’ve developed a number of functions that I use to simplify particular “hard to type” commands. This has made for some good scripting exercises, but more importantly, it has served as a great time saver when performing certain routine tasks. In this post, I am going to share some code that I use to manage disconnected Exchange mailboxes.

Finding Disconnected Mailboxes

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 {

[CmdletBinding()]

param(

[Parameter(Position=0, Mandatory=$false)]

[System.String]

$Name = ‘*’

)

 

$mailboxes = Get-MailboxServer

$mailboxes | %{

$disconn = Get-Mailboxstatistics -Server $_.name | ?{ $_.DisconnectDate -ne $null }

$disconn | ?{$_.displayname -like $Name} |

Select DisplayName,

@{n=”StoreMailboxIdentity”;e={$_.MailboxGuid}},

Database

}

}

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:

Deleting Disconnected Mailboxes

Once you have the Get-DisconnectedMailbox function working, you can use the following to delete the disconnected mailboxes.

Remove-StoreMailbox -Database $DatabaseName -Identity $MailboxID -MailboxState SoftDeleted

Where the $DatabaseName is the source database and the $MailboxID is the full StoreMailboxID name.

 

Leave a Reply

Help spread the word

1456 Greenwood Ct. | Bethlehem, PA 18015 | 484.707.1959 | me@thomaskay.me
©2018 All Rights Reserved. Content may not be used without prior express written consent.