Move Mailboxes to Target Mailbox DatabaseDescription
This script takes in a CSV file and uses the aliases to move associated mailboxes to a specified mailbox database.
Usage
Copy the script code below into a text file and name it MoveMailboxes.ps1. Populate a csv file with aliases of the users whose mailboxes you would like to move. In the Exchange Management Shell navigate to the directory in which you saved the script and run the command below:
MoveMailboxes.ps1 aliases.csv -TargetDatabase "mailbox database name"
Sample Script
$mailbox = $null
$database = $null
$existingMailboxes = @()
$nonexistingAliases = @()
if ($args.Length -lt 3)
{
write-host "Usage: MoveMailboxes.ps1 aliases.csv [-TargetDatabase [<>]]"
return
}
## Check if the given target database exists
$targetdatabase = $args[2] -replace '"',""
$database = get-mailboxdatabase $targetdatabase
if ($database -eq $null)
{
write-host "The target database doesn't exist:" $targetdatabase
return
}
$data= import-csv -path $args[0]
## Check if there is a mailbox associated with the given alias
foreach( $i in $data )
{
$mailbox = get-mailbox | where {$_.alias -eq $i.alias}
if ($mailbox -eq $null) {
$nonexistingAliases += $i.alias
} else {
$existingMailboxes += $mailbox
}
}
write-host "No mailbox associated with below aliases: "
$nonexistingAliases
write-host "Move below mailboxes to the target database:" $targetdatabase
$existingMailboxes
$existingMailboxes | Move-Mailbox -TargetDatabase $targetdatabase
Disclaimer: The sample scripts are meant to serve as examples and may need modifications before they will work in your environment. The authors of the script are not responsible for any negative outcome that may result from using them.
|