Microsoft Exchange - Mailbox Merge Script

Script: Merge-Mailbox

Type: ExchangePowerShell

Applies To: Exchange Server 2010, Exchange Server 2013, Exchange Server 2016, Exchange Server 2019

Created By: Sreejith Vu                                                 Status : ***Tested successfully in Exchange 2016***

==========================================================================

Description:

In our exchange server world we all get in to a situation like one user get two mailboxes and they will request merge it to one mailbox. Also they will ask us to add all aliases from the duplicate mailbox to the active mailbox.

So normally what we do is export the duplicate mailbox to a pst and then import it to the active mailbox.

Then take backup of all aliases from duplicate mailbox and disable or remove the duplicate mailbox. Once disabled we need to add the exported aliases to the active mailbox.

This is time consuming process and we need to perform all these steps manually by running respective commands in the shell or through exchange admin center.

So i wrote a script to perform all these tasks in a single line command. The script will perform below items for you,

  • Export duplicate mailbox to pst in a shared path.
  • Import PST to active mailbox.
  • Add all aliases from duplicate mailbox to the active mailbox.
  • Disable the duplicate mailbox.
This script will give you the percentage status on each stage and successfull completion message in the screen once each tasks completed.

You can use this script like exchange inbuilt command formats.

Sample script format will be like this,

Merge-Mailbox.ps1 -Exportmailbox Duplicateaccount -Importmailbox Activeaccount

Here you go.......!!!!!! Copy this to a notepad and save it as Merge-Mailbox.ps1

==========================================================================

    <#
    .SYNOPSIS
    Mailbox Consolidation.

    .DESCRIPTION
    This function uses exchange cmdlets to Merge two mailboxes.

    .PARAMETER ExportMailbox
    Source or Duplicate mailbox.

    .PARAMETER ImportMailbox
    Target or Active mailbox.

    .Example
    Merge-Mailbox.ps1 -Exportmailbox Duplicateaccount -Importmailbox Activeaccount
    Created By: Sreejith Vu
    #>

    
[CmdletBinding()]


    Param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true,position=0)]
        [string]$ExportMailbox,
        [parameter(Mandatory=$true,ValueFromPipeline=$true,position=1)]
        [string]$ImportMailbox
          

    )

    Begin{
# Variables

$pstpathparent ="set pst shared path here first"
}

    Process{

# PST Path subfolder Creation
$PSTPath = Join-Path $pstpathparent -ChildPath "$($ExportMailbox).pst"
# Start duplicate mailbox export

Write-host "Starting Duplicate Mailbox Export" -ForegroundColor Yellow
New-MailboxExportRequest -Mailbox $ExportMailbox -FilePath $PSTPath
Do{

$Exportpercentcomplete = (Get-MailboxExportRequest -Mailbox $ExportMailbox | Get-MailboxExportRequestStatistics).Percentcomplete
if ($Exportpercentcomplete -ne 100)
{
Write-Host -Object "Export mailbox to PST'$($ExportMailbox)' $($Exportpercentcomplete) percent completed." -ForegroundColor Green
}
else {
Write-host "Mailbox Export Completed Successfully" -ForegroundColor Green
     }
  }
Until ($Exportpercentcomplete -eq 100)
        
Start-Sleep -Seconds 1200

# PST import to orginal mailbox

Write-host "Starting PST Import to target mailbox" -ForegroundColor Yellow
New-MailboxImportRequest -Mailbox $ImportMailbox -FilePath $PSTPath -BadItemLimit 'unlimited' -AcceptLargeDataLoss -ConflictResolutionOption KeepAll
Do{
$Importpercentcomplete = (Get-MailboxImportRequest -Mailbox $ImportMailbox | Get-MailboxImportRequestStatistics).Percentcomplete

if ($Importpercentcomplete -ne 100)
{
Write-Host -Object "Import PST to mailbox'$($ImportMailbox)' $($Importpercentcomplete) percent completed." -ForegroundColor Green
}
else {
Write-host "Mailbox Import Completed Successfully" -ForegroundColor Green
     }
  }
Until ($Importpercentcomplete -eq 100)
# Adding all aliases from duplicate mailbox to orginal mailbox.

Write-host "Add duplicate mailbox alias to active mailbox" -ForegroundColor Yellow
$addresses1= (get-mailbox $ExportMailbox).emailaddresses
$addresses2= (get-mailbox $ImportMailbox).emailaddresses
$addresses = $addresses1 + $addresses2
Disable-mailbox $ExportMailbox -confirm: $false 
Set-Mailbox $ImportMailbox -EmailAddresses $addresses
Write-host "Email address imported successfully" -ForegroundColor Green
}

==========================================================================

Thank you                                                                                                                             Sreejith Vu

Comments

Post a Comment