Powershell script for Exchange Admins to Move Mailboxes based on Toatal Size

Sometime there could be requirement where we wants to move certain amount of data out of a database, below script is helpful where we wants to calculate how many mailbox fill in the required amount of Data and then wants to put them on move request.

below are the parameters required.

[int]$SizeToMoveInGB,    # Size of DATA in GB like 100 GB
     $sDB,               # Source Database
     $sort,              # to sort mbx based on size.
     $TDB,               # Target Database
     $move=$false,       # to place the Mailbox on Move
     $MRS,               # to assign MRS instance Server for move request
     $BIL                # Bed Item Limit

Usage Examples:
# Get the no of Mailbox and there list only.
>GetMbxToMoveFromDB -SDB TestDB -SizeToMoveInGB 100 

# Sort the Mailbox based on the Size and Get the no of Mailbox and there list only.
>GetMbxToMoveFromDB -SDB TestDB -SizeToMoveInGB 100 -Sort True

# Get the no of Mailbox and place them on Move below command will move 100 GB out of TestDB
>GetMbxToMoveFromDB -SDB TestDB -SizeToMoveInGB 100 -move $true -Tdb TestDB2 -MRS CAS1 -BIL 10

# Sort the Mailbox based on the Size and Get the no of Mailbox and there list only.
>GetMbxToMoveFromDB -SDB TestDB -SizeToMoveInGB 100 -move $true -Tdb TestDB2 -MRS CAS1 -BIL 10 -Sort $True


function GetMbxtoMovefromDB {

     param(
     [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
     [int]$SizeToMoveInGB,
     $sDB,
     $sort,
     $TDB,
     $move=$false,
     $MRS,
     $BIL                  
     )
$db=$sdb
write-host "
Getting Mailbox to move Based on Size selected.."

if ($sort) 
{
$MBXs=Get-MailboxStatistics -Database $DB |? {$_.DisplayName -notlike "SystemMailbox*"} | sort TotalItemSize }
else {$MBXs=Get-MailboxStatistics -Database $DB |? {$_.DisplayName -notlike "SystemMailbox*"} 
}

# Empty array to Save Mailbox to Move
$MbxtoMove=@()

#Collect size in below container
$tempSize=@()

#Required Max Size in MB
[int]$SizetoMoveinMB=$SizeToMoveInGB*1024 
$a=0
do { 
 $tempsize += $mbxs[$a].TotalItemSize.Value.ToMb()
 $Tempsize = ($Tempsize | Measure-Object -Sum).Sum
 $A++
 if ($tempsize -lt $SizetoMoveinMB)
    {        
     $MbxtoMove+=$mbxs[$a].DisplayName        
    } Else {
           $A--
           #Remove Last Name
           #$MbxtoMove -= $mbxs[$a].DisplayName
           $tempsize=($tempsize - $mbxs[$a].TotalItemSize.Value.ToMb())
           break
    }           
  }while($tempsize -lt $SizetoMoveinMB)

 $total=[math]::Round(($tempsize/1024),2) 
 
 if ($sort){ 
    write-host "No Of Mailbox Selected:"$MbxtoMove.count
    write-host "TotalSize Selected:$total"GB
    } else {
      write-host "No Of Mailbox Selected:"$MbxtoMove.count
      write-host "TotalSize Selected:$total"GB -n
      Write-Host " Next Mailbox Size is" $mbxs[$a].TotalItemSize.Value.ToGB() "GB try `'-sort:`$true' switch to sort mbx based on size" -f Yellow 
   }
 $M=$MbxtoMove | select -index (0..$(($mbxtomove.count)-1)) | get-mailbox | ? {$_.Database -like $db -and $_.Name -notlike "SystemMailbox*"}
 write-host "preparing list.."
 $b=@()
 
 foreach ($u in $M) { 
 #write-host "host" $u.Alias
 $ff = Get-MailboxStatistics $u.Alias | select DisplayName, TotalITemSize, Database, @{n="alias";e={$U.alias}} 
 $B += $ff
 }
 if (!$move){
 $b
 Write-host "
To Move the above Mailbox add param `'-Move:`$True''-tdb exampleDB'`-MRS 'exampleSrv' `'-BIL 30'" -f Yellow
 } else {
 write-host "Placing selected Mailbox on moves"
 foreach ($user in $b) 
    {
     New-MoveRequest $user.Alias -TargetDatabase $TDB -MRSServer $MRS -BadItemLimit $BIL 
    }
 } 
}

Comments