Simplify user tasks like bulk creation, updates, password resets, deletions, license checks & more — all from one place.
🚀 Launch ToolkitCreating multiple security groups manually from the Microsoft 365 admin center can be time-consuming and error-prone—especially in large organizations. With Graph PowerShell automation, administrators can quickly and reliably provision mail-disabled security groups in bulk, saving time and ensuring consistency. This script-driven approach is especially useful during large deployments, tenant migrations, or onboarding scenarios.
📌 Note: The following script is used to bulk-create security groups in Microsoft 365. It uses a CSV file containing group details and loops through each entry to create security groups using Graph PowerShell.
⚠️ Important: To create security groups (not Microsoft 365 groups), ensure that the following fields are specified correctly:
If these are omitted or misconfigured, the script will default to creating Microsoft 365 Unified groups instead of security groups.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All"
# Import the CSV
$groups = Import-Csv -Path "MailDisabledSecurityGroups.csv"
# Create each group
foreach ($group in $groups) {
$BodyParams = @{
displayName = $group.displayName
mailEnabled = $false
mailNickname = $group.mailNickname
securityEnabled = $true
groupTypes = @()
}
try {
New-MgGroup -BodyParameter $BodyParams
Write-Host "âś… Created: $($group.displayName)" -ForegroundColor Green
} catch {
Write-Host "❌ Failed: $($group.displayName)" -ForegroundColor Red
Write-Host $_.Exception.Message
}
}
CSV File Details
displayName,mailNickname
Security Group 201,secgroup201
Security Group 202,secgroup202
Security Group 203,secgroup203
Security Group 204,secgroup204
Security Group 205,secgroup205
Uses Connect-MgGraph with Group.ReadWrite.All delegated permissions to authenticate.
Reads the group data from a local CSV file named MailDisabledSecurityGroups.csv.
Each row is processed inside a foreach loop where:
Each group is created using New-MgGroup. If successful, a success message appears; if it fails, the error is shown.
Note: You cannot create bulk create mail enabled security groups or distribution lists using Graph PowerShell. If you try, you’ll get the following error: New-MgGroup : Cannot Create a mail-enabled security groups and or distribution list. You need to use Exchange PowerShell for this.
Error | Cause | Solution |
---|---|---|
Access Denied | Missing permissions | Ensure Group.ReadWrite.All is granted and consented |
MailNickname already in use | Duplicate mail nickname | Use unique mailNickname values in CSV |
Invalid group type | Incorrect or missing groupTypes | Always set groupTypes = @() for security groups |
Connect-MgGraph fails | Auth issue | Ensure your account has delegated admin rights |
This script enables administrators to automate the bulk creation of mail-disabled security groups using Graph PowerShell with accuracy and efficiency. By enforcing proper flags like securityEnabled = $true and groupTypes = @(), it ensures correct group type provisioning and avoids misconfiguration.
Incorporating such automation not only reduces manual workload but also promotes consistent governance across the organization’s Microsoft 365 environment.
© m365corner.com. All Rights Reserved. Design by HTML Codex