Microsoft 365 Dynamic Mail Groups

Yesterday was an interesting one for work. Our custom Dynamic Mail groups that had worked for well over a year stopped working, and we had to figure out how to redo them quickly. The best I could figure, Microsoft has made a change in how those custom rules worked, and nothing was being populated for our rule where we were attempting to determine the group membership based on the domain of the Primary SMTP address.

We ended up pivoting to some of Microsoft’s pre-canned rules determining membership based on the company name field in AD and AzureAD. This was not a huge issue for half of our users as some already had this field populated with correct information. For the rest, I had to whip up a quick script to populate this field based off of their Primary SMPT domain. I’m sharing this Power Shell script below in case anyone comes across this post with a similar issue.

#Import the AzureAD module

Import-Module AzureAD

#Connect to Azure AD

Connect-AzureAD

#Define the domain and the new company name

$domain = “domain.com”
$newCompanyName = “Company”

#Get the user accounts with the specified domain

$users = Get-AzureADUser -All $true | Where-Object { $_.UserPrincipalName -like “*@$domain” }

#Update the company name for each user

foreach ($user in $users) {
Set-AzureADUser -ObjectId $user.ObjectId -CompanyName $newCompanyName
Write-Output “Updated company name for user: $($user.UserPrincipalName)”
}

Write-Output “Company name update completed.”

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *