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 protected]" -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
}
                                   }
 
 }

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 7.50 out of 5)
Loading...