Exchange Online – merge a soft deleted mailbox into an active mailbox

Hi! hope you are well… I used this script yesterday and thought “I must post about that!” So here it is… 😃

This can be a good way to deal with an employee leaving, or if you end up with a dual-mailbox scenario, although Microsoft have made that much less likely since adding the Hybrid Exchange option to AD Connect a while back…

Right, enough kafuffle – onward! 👀

Suppose a user leaves – your off-boarding procedure may include something like:

  • Exporting the mailbox to a PST file which is stored somewhere
  • Converting the mailbox to a Shared Mailbox and giving a another user access to it

But what if there is an incoming user that would benefit from having the existing data in their newly created mailbox? It might be a training coordinator or HR administrator’s mailbox that has a heap of relevant information that the incoming user can search through for information. This is much easier to use when it is in your main mailbox, rather than an attached shared mailbox (or worse a PST file!).

This script will connect to Exchange Online and prompt you with a list of soft-deleted mailboxes (these are the ones that have been deleted but remain for 30 days before being permanently deleted). Select the recently deleted mailbox and click OK. Now you are prompted with a list of all active mailboxes. Select the mailbox to merge the data into and click OK. Now the soft-deleted mailbox will be merged into the active mailbox. Superb!

Make sure you have run ‘Install-Module ExchangeOnlineManagement‘ so you can run the script successfully.

Here’s the code:

# set variables to null
$source = $null
$target = $null

#Connect to EOL...
Try {
    Connect-ExchangeOnline -ShowBanner:$false
}
    Catch {
        Write-Warning "Failed to connect to Exchange Online!"
        Read-Host "Press a key to exit..."
        Exit
    }

# prompt to select source mailbox
$source = Get-Mailbox -SoftDeletedMailbox -ResultSize unlimited | Select DisplayName,ExchangeGuid,PrimarySmtpAddress,ArchiveStatus,DistinguishedName | Out-GridView -Title "Select source soft-deleted mailbox and click OK..." -PassThru

if ($source -eq $null) {
        Write-Warning "No source selected. Press any key to exit..."
        Read-Host
        Exit
}

# prompt to select target mailbox
$target = Get-Mailbox -ResultSize unlimited | Select Name,PrimarySmtpAddress,DistinguishedName | Out-GridView -Title "Select target mailbox and click OK..." -PassThru

if ($target -eq $null) {
        Write-Warning "No target selected. Press any key to exit..."
        Read-Host
        Exit
}

# start merging source into target
Try {
    New-MailboxRestoreRequest -SourceMailbox $source.DistinguishedName -TargetMailbox $target.PrimarySmtpAddress -AllowLegacyDNMismatch
}
    Catch {
        $errormsg = $Error[0].Exception.Message
        Write-Warning "An error occured. $errormsg. Press any key to exit..."
        Read-Host
        Exit
    }

As usual please let me know if you have any trouble or have a scenario that warrants a modified solution and I’ll try to help.

Until next time! 🍻✌😎✌🍻

Loading

2 thoughts on “Exchange Online – merge a soft deleted mailbox into an active mailbox”

  1. I didn’t know it was possible it use out-gridview as a selection UI – very handy!

    There’s been recent occasions where this merge method could have saved us heaps of time.

    As an employee leaves, we disable them, convert mailboxes to ‘shared’, strip any licenses, then pop them into a queue scheduled for deletion a few months later.

    1. Agreed! Being introduced to this was as happy a moment as when I first learned about out-gridview!

      I like what you are doing with the offboarding… much better than looking 5 years later and realizing you have 13.4TB of shared mailbox data nobody uses (hopefully not being backed up)!!

Leave a Reply to RobV Cancel Reply

Your email address will not be published. Required fields are marked *

Scroll to Top