This guide covers how to use the New-MgGroup cmdlet in Microsoft Graph PowerShell to create Microsoft 365 groups. Learn to set properties like display name, mail nickname, and group type with examples for single and bulk creation
The New-MgGroup cmdlet in Microsoft Graph PowerShell is a powerful tool for creating Microsoft 365 Groups. This article will guide you through the prerequisites, syntax, usage examples, cmdlet tips and possible errors and solutions.
Install-Module Microsoft.Graph -Scope CurrentUser.Connect-MgGraph -Scopes "Group.ReadWrite.All"The basic syntax of the New-MgGroup cmdlet is as follows:
New-MgGroup -DisplayName <String> -MailNickname <String> -GroupTypes <String[]> -MailEnabled <Boolean> -SecurityEnabled <Boolean>Example 1: Create a Security Group
New-MgGroup -DisplayName "Security Group 1" -MailNickname "SecGroup1" -SecurityEnabled -MailEnabled:$falseExample 2: Create an Office 365 Group (Unified Group)
New-MgGroup -DisplayName "Office 365 Group 1" -MailNickname "O365Group1" -GroupTypes "Unified" -MailEnabled:$trueExample 3: Creating Multiple Office 365 Groups by Reading Data from a CSV File
You can create multiple Office 365 groups at once by reading group details from a CSV file. Here’s how you can do it:
CSV File Example:
    DisplayName,MailNickname,Description
    HR Team 1000,HRTeam1000,Group for HR 1000 department
    Sales Team 1000,SalesTeam1000,Group for Sales 1000 department
    IT Team 1000,ITTeam1000,Group for IT 1000 department
PowerShell Script:
    $groups = Import-Csv -Path "C:\Path\To\Groups.csv"
    foreach ($group in $groups) {
        $groupParams = @{
        DisplayName     = $group.DisplayName
        MailNickname    = $group.MailNickname
        Description     = $group.Description
        GroupTypes      = @("Unified")
        MailEnabled     = $true
        SecurityEnabled = $false
    }
        New-MgGroup -BodyParameter $groupParams
    }
-GroupTypes to "Unified". For security groups, you can omit this parameter or set it to @().$true for security groups and $false for Office 365 Groups.$true for Office 365 Groups and $false for security groups.| Error | Cause | Solution | 
| Duplicate MailNickname Error: | Another object with the same value for property mailNickname already exists. | Ensure the MailNickname you are using is unique. You can check existing groups with the following command: Get-MgGroup -Filter "mailNickname eq 'MailNicknameToCheck'" | 
| Authorization_RequestDenied | Insufficient privileges to complete the operation. | Ensure you have the required permissions by running: Connect-MgGraph -Scopes "Group.ReadWrite.All" | 
| Duplicate Group Name | Attempting to create a group with a name that already exists within the tenant can result in an error. | Group names must be unique within the Microsoft 365 environment. Before creating a group, check for existing group names:  | 
| Invalid GroupTypes Error: | Ensure you set the GroupTypes parameter correctly. | Use "Unified" for Office 365 Groups and omit or set it to @() for security groups. | 
DisplayName,MailNickname,MailEnabled,SecurityEnabled
Group 1,Group1,$true,$false
Group 2,Group2,$true,$false$Groups = Import-Csv -Path "C:\Path\To\File.csv"
    foreach ($Group in $Groups) {
        $Body = @{
            displayName = $Group.DisplayName
            mailNickname = $Group.MailNickname
            mailEnabled = [bool]::Parse($Group.MailEnabled)
            securityEnabled = [bool]::Parse($Group.SecurityEnabled)
            groupTypes = @("Unified")
        }
        New-MgGroup -BodyParameter $Body
    }$groupParams = @{
    DisplayName     = "Project Team"
    MailNickname    = "projectteam"
    GroupTypes      = @("Unified")
    MailEnabled     = $true
    SecurityEnabled = $false
}
$group = New-MgGroup -BodyParameter $groupParams
# Add an owner to the group
New-MgGroupOwner -GroupId $group.Id -DirectoryObjectId "<OwnerObjectId>"
$groupParams = @{
    DisplayName                      = "Dynamic Group"
    MailNickname                     = "dynamicgroup"
    GroupTypes                       = @("Unified", "DynamicMembership")
    MailEnabled                      = $true
    SecurityEnabled                  = $false
    MembershipRule                   = '(user.department -eq "Sales")'
    MembershipRuleProcessingState    = "On"
}
New-MgGroup -BodyParameter $groupParamsBy understanding these aspects of the New-MgGroup cmdlet, administrators can effectively create and manage groups within their Microsoft 365 environments, tailoring them to meet organizational needs.
 
                            
                                 
                            
                                 
                            
                                 
                            
                                 
                                 
                            mailNickname and displayNameNew-MgGroup, both displayName and mailNickname are required.400 Bad Request error.
                              mailEnabled and securityEnabledmailEnabled = $true and securityEnabled = $false → Microsoft 365 GroupmailEnabled = $false and securityEnabled = $true → Security GroupmailEnabled = $true and securityEnabled = $true → Mail Enabled Security GroupNew-MgGroup, only Microsoft 365 Groups (with groupTypes set to "Unified") come with collaboration features like a shared mailbox, calendar, Planner, and Teams integration.The New-MgGroup cmdlet is a versatile and essential tool for managing Microsoft 365 Groups. By understanding its prerequisites, syntax, and usage you can efficiently create and manage groups within your organization. Remember to handle common errors and use cmdlet tips to streamline your group management tasks.
© m365corner.com. All Rights Reserved. Design by HTML Codex