Script to reassign mailbox to a new user


In some organizations when a user leaves the company or is moved to a new role their existing mailbox needs to be reassigned to their replacement. In other words, the mailbox belongs to the role and not the person.

If you just disabled the mailbox many Exchange properties are lost, include the e-mail addresses and LegacyExchangeDN address (which is used by Outlook and stored in the Outlook nickname cache for message delivery). So these addresses need to be manually copied to the new mailbox, the LegacyExchangeDN needs to be added as an X.500 (X500) address to allow users to reply to past messages from the mailbox and to prevent message delivery issues when using Outlook.

So I created this script to simplify the process.

Script: Reassign-Mailbox.ps1

#******************************************************************************************

# Created by Jason Sherry (izzy@izzy.org) 3/15/2012
#
# Copies an existing mailbox properties and reassigns it to a new user and restores properties after reassigning
# Source: http://izzy.org/scripts/Exchange/Admin/Reassign-Mailbox.ps1
#Import-Module ActiveDirectory # Only needed if copying groups, which script doesn't support yet

param(
[Parameter(Mandatory = $true)]
[String]$CurrentUser,
[Parameter(Mandatory = $true)]
[String]$NewUser
)

Try {
$Mailbox = Get-Mailbox $CurrentUser -ErrorAction Stop

Write-Host "Got mailbox info for" $Mailbox.DisplayName

# $Groups = (GET-ADUSER -Identity $CurrentUser-Properties MemberOf | Select-Object MemberOf).MemberOf
}
Catch {
"Error:" ; $error[0].Exception.Message
return
}

Write-Host "Existing Mailbox Details:"

$MBXStats = get-mailboxstatistics $Mailbox | select *
$MBXStats
#$MBXPerms = get-mailboxpermission $Mailbox | select *
#$MBXPerms

Disable-Mailbox $Mailbox.alias -confirm:$false

Write-Host "Waiting 15 seconds for cache refresh"

start-sleep 15

Connect-Mailbox $Mailbox.exchangeguid -database $Mailbox.database -Alias $Mailbox.mailnickname -user $NewUser

$NewMailbox = Get-Mailbox $NewUser

$Mailbox.EmailAddresses += [Microsoft.Exchange.Data.CustomProxyAddress]("X500:" + $Mailbox.legacyExchangeDN)

Set-Mailbox $NewMailbox.identity -emailaddresspolicyenabled $False
Set-Mailbox $NewMailbox.identity -emailaddresses $Mailbox.emailaddresses

Set-Mailbox $NewMailbox.identity -emailaddresspolicyenabled $True
#******************************************************************************************

Usage:
.\Reassign-Mailbox.ps1

Example:
.\Reassign-Mailbox.ps1 JohnDoe JaneDoe

The above example would do the following:
1. Copy the e-mail addresses from the JohnDoe
2. Disconnect the mailbox from this account JohnDoe
Note: When a mailbox is disconnected many properties for the mailbox are lost, include the e-mail addresses for it. The script copies the mailbox information to a variable first to prevent the loss of these properties.

3. Reconnect the mailbox to JaneDoe
4. Copy the e-mail addresses and X500 address into the newly reattached mailbox for JaneDoe

This entry was posted in Exchange, Microsoft, Technical and tagged , , . Bookmark the permalink.

2 Responses to Script to reassign mailbox to a new user

  1. DDog says:

    I see that you’re obtaining the groups and the MailboxPermissions but not doing anything with them. How can you use that to apply to the new mailbox if that were desired? thanks.

    Like

    • jasonsherry says:

      Parsing permissions is a challenge and I haven’t looked into doing anything with them. Those lines are commented out in the script since I don’t do anything with them, but left them in just in case others wanted to.

      Like

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