Source :
Having worked with Exchange 2007/2010 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 explanitory. 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:
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"
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. For a good example of this, check out Nitin Gupta's post on removing disconnected mailboxes.
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 { [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter(Position=0, ValueFromPipelineByPropertyName=$true, Mandatory=$true)] [System.String] $StoreMailboxIdentity, [Parameter(Position=1, ValueFromPipelineByPropertyName=$true, Mandatory=$true)] [System.String] $Database ) 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'}
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.