
Connect to Azure AD
Connect-AzureAD
Define the Azure AD groups
$group1 = “Group1”
$group2 = “Group2”
Get all users with a primary mailbox and archive mailbox size
$users = Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySmtpAddress, ArchiveName, ArchiveQuota, PrimaryMailbox
Loop through each user
foreach ($user in $users) {
# Calculate the total mailbox size
$totalSize = $user.PrimaryMailbox.TotalItemSize.Value.ToBytes() + $user.ArchiveSize.Value.ToBytes()
# Check if the mailbox size is less than 45GB
if ($totalSize -lt 45GB) {
# Add the user to Group1
Add-AzureADGroupMember -ObjectId $group1 -RefObjectId $user.PrimarySmtpAddress
# Wait for 5 minutes
Start-Sleep -Seconds 300
# Remove the user from Group1 and add to Group2
Remove-AzureADGroupMember -ObjectId $group1 -MemberId $user.ObjectId
Add-AzureADGroupMember -ObjectId $group2 -RefObjectId $user.ObjectId
# Reprocess the Azure AD license for the user
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $user.AssignedLicenses
}
}
Azure AD User Groups.
Powershell script to add users to Azure AD group based on mailbox size condition of <45gb or >45gb based on primary mailbox and archive mailbox size. Users with 45gb+ should be removed from group1, then wait for 5 min then add to group2 and reprocess Azure AD license for the user.
Here’s a Powershell script that should accomplish what you’re looking for:
powershellCopy code# Connect to Azure AD
Connect-AzureAD
# Define the Azure AD groups
$group1 = "Group1"
$group2 = "Group2"
# Get all users with a primary mailbox and archive mailbox size
$users = Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, PrimarySmtpAddress, ArchiveName, ArchiveQuota, PrimaryMailbox
# Loop through each user
foreach ($user in $users) {
# Calculate the total mailbox size
$totalSize = $user.PrimaryMailbox.TotalItemSize.Value.ToBytes() + $user.ArchiveSize.Value.ToBytes()
# Check if the mailbox size is less than 45GB
if ($totalSize -lt 45GB) {
# Add the user to Group1
Add-AzureADGroupMember -ObjectId $group1 -RefObjectId $user.PrimarySmtpAddress
# Wait for 5 minutes
Start-Sleep -Seconds 300
# Remove the user from Group1 and add to Group2
Remove-AzureADGroupMember -ObjectId $group1 -MemberId $user.ObjectId
Add-AzureADGroupMember -ObjectId $group2 -RefObjectId $user.ObjectId
# Reprocess the Azure AD license for the user
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $user.AssignedLicenses
}
}
A few notes on how this script works:
- First, we connect to Azure AD using
Connect-AzureAD
. - We define the names of the two Azure AD groups we’ll be using.
- We get a list of all users with a primary mailbox and archive mailbox size using
Get-Mailbox
. - We loop through each user, calculating the total mailbox size by adding the size of their primary mailbox and archive mailbox.
- If the total mailbox size is less than 45GB, we add the user to Group1.
- We then wait for 5 minutes using
Start-Sleep
. - After 5 minutes, we remove the user from Group1 and add them to Group2 using
Remove-AzureADGroupMember
andAdd-AzureADGroupMember
. - Finally, we reprocess the Azure AD license for the user using
Set-AzureADUserLicense
.
Note that this script assumes that you have already assigned licenses to the users in question. If you haven’t, you’ll need to do that before running this script. You’ll also need to make sure that the Azure AD module is installed on the machine where you’re running this script.