Clear Disconnected Mailboxes in Exchange 2010

After clearing hundreds of move requests in Office 365 via PowerShell, I had to clear all of the disconnected mailboxes in Exchange 2010 those moves created. However, when I ran this command…

Get-MailboxStatistics -Database "Mailbox Database" | where {$_.DisconnectReason -eq "SoftDeleted"} | ForEach-Object {Remove-StoreMailbox -Database $_.database -Identity $_.mailboxguid -MailboxState SoftDeleted}

…this error came up…

Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.

Come to find out, “ForEach-Object” doesn’t work all that well when pipelining the output into it via Exchange 2010 because it tries to remove all of the mailboxes at once instead of individually. To resolve, the single command had to be broken up into two commands. The first command gathers the variable data. and the second command executes the remove for each value captured by the variable. I don’t exactly know why it works this way and not the other but it does.

$DisconnectedMailbox = Get-MailboxStatistics -Database "Mailbox Database" | where {$_.DisconnectReason -eq "SoftDeleted"}

$DisconnectedMailbox | ForEach-Object {Remove-StoreMailbox -Database $_.database -Identity $_.mailboxguid -MailboxState SoftDeleted}

Reference(s):

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s