Managing dynamic membership groups one by one in the Microsoft 365 admin center can quickly become tedious—especially in large environments. A much better approach is to automate the process and create multiple dynamic Microsoft 365 groups in bulk using Microsoft Graph PowerShell.
In this article, we’ll walk through a simple and effective Graph PowerShell script that reads group details from a CSV file and creates Microsoft 365 (Unified) dynamic membership groups in bulk.
Try the M365Corner Microsoft 365 Reporting Tool — your DIY pack with 20+ out-of-the-box M365 reports for Users, Groups, and Teams.
Before running the script, ensure the following:
Connect-MgGraph -Scopes "Group.ReadWrite.All","Directory.ReadWrite.All"
The script reads group information from a CSV file. Your CSV must include the following columns:
DisplayName,Description,MailNickname,MembershipRule
HR – Employees,Dynamic group for HR users,hremployees,(user.department -eq "HR")
IT – Users,Dynamic group for IT users,itusers,(user.department -eq "IT")
US Users,All users located in the US,ususers,(user.country -eq "United States")
Column Explanation
| Column | Description |
|---|---|
| DisplayName | Display name of the Microsoft 365 group |
| Description | Description of the group |
| MailNickname | Unique mail alias (no spaces) |
| MembershipRule | Dynamic membership rule |
Below is the exact script used to bulk create dynamic Microsoft 365 groups by reading data from a CSV file.
$CsvPath = "D:\DynamicM365Groups.csv"
$Groups = Import-Csv $CsvPath
foreach ($Group in $Groups) {
Write-Host "Creating group: $($Group.DisplayName)" -ForegroundColor Cyan
try {
$Params = @{
DisplayName = $Group.DisplayName
Description = $Group.Description
GroupTypes = @("Unified", "DynamicMembership")
MailEnabled = $true
MailNickname = $Group.MailNickname
SecurityEnabled = $false
MembershipRule = $Group.MembershipRule
MembershipRuleProcessingState = "On"
}
New-MgGroup -BodyParameter $Params
Write-Host "✔ Successfully created: $($Group.DisplayName)" -ForegroundColor Green
}
catch {
Write-Host "✖ Failed to create: $($Group.DisplayName)" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Yellow
}
}
Your CSV file should read as follows with the appropriate headers:
DisplayName,Description,MailNickname,MembershipRule
HR – Full Time Employees,Dynamic group for full-time HR users,hrfte,(user.department -eq "HR")
IT – Contractors,Dynamic group for IT contractors,itcontract,(user.department -eq "IT")
US Users One More,All users located in the US,ususersonemore,(user.country -eq "USA")
Let’s break this down in simple terms:
Once you’re comfortable with this script, you can extend it in several useful ways:
These enhancements are especially useful when running the script in production tenants.
| Error | Cause | Solution |
|---|---|---|
| Duplicate MailNickname Error | Another object with the same value for property mailNickname already exists | Ensure MailNickname values in the CSV are unique across the tenant. |
| Invalid Membership Rule | DynamicGroupQueryParseError | Verify that the membership rule uses only supported dynamic rule attributes and correct syntax. |
| Insufficient Permissions | Insufficient privileges to complete the operation | Reconnect to Microsoft Graph with Group.ReadWrite.All and Directory.ReadWrite.All permissions. |
Bulk creating Microsoft 365 dynamic membership groups using Graph PowerShell is a clean, repeatable, and admin-friendly approach—especially when combined with CSV input. This script keeps things simple, avoids unnecessary complexity, and works well for real-world tenant management.
If you manage multiple departments, regions, or user categories, this approach can save significant time while ensuring consistency across your Microsoft 365 environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex