Script to Move Mailboxes through Powershell and move AD group when finished

PowerShell logo

$users = @("user1","user2")
 
foreach ($user in $users) {
 
$PSEmailServer = "server.domain.local"
 
#Move
New-MoveRequest -identity $user -TargetDatabase "Mailbox Database 1"
 
$MoveStats = Get-MoveRequest -identity $user | Get-MoveRequestStatistics
$MoveStatus = $MoveStats.Status
 
WHILE ($MoveStatus -eq "Queued" -or $MoveStatus -eq "InProgress" -or $MoveStatus -eq "CompletionInProgress") { Start-Sleep 30 
                                     $MoveStats = Get-MoveRequestStatistics -identity $user | Get-MoveRequestStatistics
                                     $MoveStatus = $MoveStats.Status
                                    }
IF ($MoveStatus -eq "Failed") {             
                                  Send-MailMessage -To "email@domain.com" -From "server.domain.local" -Subject "Exchane Mailbox Mover" -Body "The Exchange Mailbox 
 
Export of the $user failed"
                                }
 
IF ($MoveStatus -eq "Completed") { 
#once completed move user from one group to other
import-module activedirectory
$usergroups = (GET-ADUSER –Identity $user –Properties MemberOf).MemberOf
if ($usergroups -match 'Usergroup') { 
Remove-ADGroupMember -Identity "Usergroup" -Members $user -Confirm:$false
Add-ADGroupMember -Identity "NewUserGroup" -Members $user
}
                                   }
 
 }

VN:F [1.9.22_1171]
Rating: 7.5/10 (2 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)
Script to Move Mailboxes through Powershell and move AD group when finished, 7.5 out of 10 based on 2 ratings

Tags: Get-MoveRequestStatistics, movemailbox, New-MoveRequest, powershell

Trackback from your site.