Forum Discussion
CWall09
Dec 09, 2021Copper Contributor
Need assistance bulk creating M365 Dynamic Groups and configuring mail properties via powershell
Hello community, I am able to bulk create M365 Groups (static membership) via the post link here - How to bulk create Microsoft 365 groups - Microsoft Community This uses the 'New-UnifiedGroup' ...
Smith-Espina
Jun 21, 2022Copper Contributor
Here is the command for bulk create dynamic M365 groups
$attribute = "Department" # Change to the attribute you would like to use
$displayNamePrefix = "M365group" # Optionally set a Prefix for the group name
$displayNameSuffix = "Dynamic" # Optionally set a Suffix for the group name
Connect-AzureAD
$users = Get-AzureADUser -All $true
$uniqueList = $users | select $attribute -Unique | ? $attribute -ne $null
$uniqueList.($attribute) | ForEach-Object {
Write-Host "Creating Group for $($_)"
$cleanName = ($_) -replace '[*\\~;(%?.:@/,&+-]' -replace ' '
$displayName = $displayNamePrefix + $cleanName + $displayNameSuffix
$args = @{
DisplayName = $displayName
Description = "Dynamic Group for $($_)"
MailEnabled = $True
MailNickname = $displayName
SecurityEnabled = $true
GroupTypes = "DynamicMembership", "Unified"
MembershipRule = "(user.$($attribute) -eq ""$($_)"")"
MembershipRuleProcessingState = "On"
}
$dg = New-AzureADMSGroup @args
Write-Host "Group Created: $displayName"
}
Reference:
https://office365itpros.com/2019/06/27/creating-dynamic-office-365-group-global-administrators/
https://martinday.co/bulk-create-dynamic-security-groups/
$attribute = "Department" # Change to the attribute you would like to use
$displayNamePrefix = "M365group" # Optionally set a Prefix for the group name
$displayNameSuffix = "Dynamic" # Optionally set a Suffix for the group name
Connect-AzureAD
$users = Get-AzureADUser -All $true
$uniqueList = $users | select $attribute -Unique | ? $attribute -ne $null
$uniqueList.($attribute) | ForEach-Object {
Write-Host "Creating Group for $($_)"
$cleanName = ($_) -replace '[*\\~;(%?.:@/,&+-]' -replace ' '
$displayName = $displayNamePrefix + $cleanName + $displayNameSuffix
$args = @{
DisplayName = $displayName
Description = "Dynamic Group for $($_)"
MailEnabled = $True
MailNickname = $displayName
SecurityEnabled = $true
GroupTypes = "DynamicMembership", "Unified"
MembershipRule = "(user.$($attribute) -eq ""$($_)"")"
MembershipRuleProcessingState = "On"
}
$dg = New-AzureADMSGroup @args
Write-Host "Group Created: $displayName"
}
Reference:
https://office365itpros.com/2019/06/27/creating-dynamic-office-365-group-global-administrators/
https://martinday.co/bulk-create-dynamic-security-groups/
JarromeAlvarez
Aug 08, 2022Copper Contributor
Very well-verse and accurate.