Disable/Enable a Mailbox – Change user account while saving mails

Sometimes you have to change a whole account but transfer the mailbox database.

There are two ways to do that :

1 – Creating the new account, creating an export of the mailbox using pst files, importing this pst file into the new mailbox

Using that method, the person which will do that will have all access to the user’s mails, not really secure..

2 (the best one) :

Using this little script 🙂

#Getting all mandatory informations about the old username
$OlduserDatabase=(Get-mailbox $olduser).Database.ToString()
$OlduserDisplayName=(Get-mailbox $olduser).DisplayName.ToString()

#Disabling and cleaning the mailbox/database
Disable-Mailbox $Olduser -confirm:$false
Clean-MailboxDatabase $OlduserDatabase
$domainusername=”$onedomain$newuser”

#Getting the mailboxdatabase of the old user
$disconnectedMailbox=Get-DisconnectedMailbox $OlduserDisplayName
$disconnectedMailbox |
Foreach-Object{
Connect-Mailbox -Identity $_.StoreMailboxIdentity -Database $OlduserDatabase -User $domainusername -Alias $newuser
}
#Here we go !
return “New user $newuser has been succesfully made”