This script was written to migrate users from one domain to another and migrate the mailboxes. Save the entire script as a .ps1 file and when you run it you will need to pass it a file name that should be in same directory as script.
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin
This function is passed the number of seconds and it displays Sleeping for 0:0:0 seconds
function CountDown($waitSeconds) {
$startTime = get-date
$endTime = $startTime.addSeconds($waitSeconds)
$timeSpan = new-timespan $startTime $endTime
write-host “`nSleeping for $waitSeconds seconds…” -backgroundcolor black -foregroundcolor yellow
while ($timeSpan -gt 0) {
$timeSpan = new-timespan $(get-date) $endTime
write-host “`r”.padright(40,” “) -nonewline
write-host $([string]::Format(“`rTime Remaining: {0:d2}:{1:d2}:{2:d2}”, `
$timeSpan.hours, `
$timeSpan.minutes, `
$timeSpan.seconds)) `
-nonewline -backgroundcolor black -foregroundcolor yellow
sleep 1
}
write-host “”
}
This section of code is what I used to get a unigue log file as I was migrating many mailboxes. You can hard code the path if desired.
$a = Get-Date
$logday = $a.Day
$logmonth = $a.Month
$logYear = $a.Year
$logHour = $a.Hour
$logMinute = $a.Minute
$logdate = $logmonth.ToString() + “_” + $logday.ToString() + “_” + $logyear.ToString() + “_” + $logHour.ToString() + $logMinute.ToString()
$logfile = “c:\conversionlog_” + $logdate + “.txt”
This section starts powershell transcript
start-transcript -path $logfile
The following code is where the file is read and the values are parsed to export the mailbox from one domain to the other. You will need a csv file with DOMAINA and DOMAINB as the two columns and for my file I used the samAccount from both domains. This code does not create the new account in the new domain it just mail enables it. But you could write that into this code if you needed that functionality.
$file = Read-Host “Type File Name (ex. Users.csv)”
# get users
$Users = import-csv $file -delimiter ‘,’ -erroraction stop
foreach ($User in $Users)
{
# Add permission for the user running script to export
Add-MailboxPermission “$($User.DOMAINA)@fqdn.olddomain” -User:‘USER TO RUN EXPORT’ -AccessRights:FullAccess
# Write to screen what we just did
Write-Host “USER NAME permission added”
# Path and Name to Export PST File
$path = “C:\PSTBackup\$($User.DOMAINA).pst”
# Export the PST from Exchange
Export-Mailbox –Identity “$($User.DOMAINA)@fqdn.olddomain” -PSTFolderPath $path -Confirm:$false
# Write to screen what we just did
Write-Host “$($User.DOMAINA) exported to C:\PSTBackup\$($User.DOMAINA).pst”
# Disable the Exchange Attributes so we can add to new domain
Disable-Mailbox “$($User.DOMAINA)@fqdn.olddomain” -DomainController “YOUR OLD DOMAIN CONTROLLER” -Confirm:$false
# Write to screen what we just did
Write-Host “$($User.DOMAIN) mailbox disabled.”
# Run a wait function to allow AD to update
Countdown 60
# Enable the new account on new domain
Enable-Mailbox -Identity “$($User.DOMAINB)@fqdn.newdomain” -Alias $User.DOMAINB -Database ‘FULL DATABASE PATH’ -Confirm:$false
# Again wait for 60 seconds for AD to update
Countdown 60
# Add permission to new mailbox to run Import
Add-MailboxPermission “$($User.DOMAINB)@fqdn.newdomain” -User:‘USER TO RUN IMPORT’ -AccessRights:FullAccess
# Again wait for 60 second for AD to update
Countdown 60
# Import the exported PST into new mailbox
Import-Mailbox -Identity “$($User.DOMAINB)@fqdn.newdomain” -PSTFolderPath $path -Confirm:$false
# Write to screen what we just did
Write-Host “$($User.DOMAINB) imported”
}
Stop the transcript for PowerShell