3 Useful Scripts with New-MgGroup in PowerShell

Creating and managing groups is an essential part of Microsoft 365 administration. Groups help organize teams, control access to resources, and streamline collaboration. With Microsoft Graph PowerShell, you can easily create, customize, and manage Microsoft 365 (Office 365) groups using the New-MgGroup cmdlet.

In this article, we’ll walk through three practical PowerShell scripts that use New-MgGroup to create Office 365 groups. Each example is simple, easy to understand, and perfect for beginners who want to automate group creation in Microsoft 365.


Prerequisites

Before running these scripts, make sure you have installed and connected to the Microsoft Graph PowerShell module:

Install-Module Microsoft.Graph -Scope CurrentUser
Connect-MgGraph -Scopes "Group.ReadWrite.All"

Once connected, you can start creating Office 365 groups using the following scripts.


Example 1: Create an Office 365 Group (Unified Group)

New-MgGroup -DisplayName "Office 365 Group 1" -MailNickname "O365Group1" -GroupTypes "Unified" -MailEnabled:$true

Explanation:

This command creates a new Microsoft 365 (Unified) group named Office 365 Group 1 with the mail nickname O365Group1.

  • The -GroupTypes "Unified" parameter specifies that this is a Microsoft 365 group (as opposed to a security group).
  • The -MailEnabled:$true property ensures the group can send and receive emails.

This is the most common way to create collaboration-ready groups for Teams, Outlook, and SharePoint.


Example 2: Create a Microsoft 365 Security Group

New-MgGroup -DisplayName "Security Group 1" -MailNickname "SecGroup1" -SecurityEnabled -MailEnabled:$false

Explanation:

This command creates a new Microsoft 365 Security group named Security Group 1 with the mail nickname O365Group1.

  • -SecurityEnabled flag is mandatory to create Microsoft 365 Security Groups
  • -MailEnabled flag should be set to false, since security groups don’t need mail id; still -MailNickname param is mandatory for successful security group creation.

Example 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
                                        

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
    }
                                        

Explanation:

This script imports a list of groups from a CSV file and creates them in Microsoft 365 automatically.

  • The Import-Csv cmdlet reads group details such as display name, nickname, and description.
  • Each entry is passed as a hashtable ($groupParams) to New-MgGroup via the -BodyParameter.
  • The script sets MailEnabled to $true and SecurityEnabled to $false, ensuring each group is created as a mail-enabled Microsoft 365 group.

Use Case: This is especially useful when you’re onboarding new teams, departments, or projects that require multiple groups at once.



Common Errors & Fixes

Error Cause Solution
Insufficient privileges to complete the operation Missing permissions Connect using Connect-MgGraph -Scopes "Group.ReadWrite.All"
Group already exists Duplicate DisplayName or MailNickname Use unique names for each group
Invalid MailNickname Contains spaces or special characters Use simple, alphanumeric nicknames

Use Case Ideas

  • Automate creation of departmental or project-based groups
  • Quickly set up collaboration groups for new employees
  • Create multiple Microsoft 365 groups from HR or onboarding spreadsheets
  • Reduce manual errors when managing large tenants

Conclusion

The New-MgGroup cmdlet makes group creation in Microsoft 365 both simple and scalable. Whether you’re setting up a single group or provisioning dozens at once from a CSV file, these examples help streamline the process and save valuable time.

With PowerShell automation, you can focus more on managing collaboration and less on repetitive setup tasks — making your administration more efficient and error-free.


Did You Know? Managing Microsoft 365 applications is even easier with automation. Try our Graph PowerShell scripts to automate tasks like generating reports, cleaning up inactive Teams, or assigning licenses efficiently.

Ready to get the most out of Microsoft 365 tools? Explore our free Microsoft 365 administration tools to simplify your administrative tasks and boost productivity.

© Your Site Name. All Rights Reserved. Design by HTML Codex