Import Microsoft 365 Groups from CSV File Using Graph PowerShell

In today's collaborative work environment, efficiently managing Microsoft 365 groups is essential for maintaining smooth operations across an organization. This article explores a powerful script that leverages the Microsoft Graph PowerShell and the Import-Csv cmdlet to automate the creation of Microsoft 365 groups from a CSV file. Using this PowerShell script, M365 administrators can streamline the process of setting up groups with precise configurations, ensuring that team setups are both swift and accurate.


Step 1: Install the Microsoft Graph PowerShell Module

Run the below command to install Microsoft Graph PowerShell module.

PowerShell cmdlet to install Graph PowerShell Module.

Step 2: Connect to Microsoft Graph

Connect to Microsoft Graph using Group.ReadWrite.All permission required to read and write Microsoft 365 groups in the organization.

PowerShell cmdlet to connect with Graph PowerShell Module.

Step 3: Define the Import-CSV PowerShell Function

We define a function called Import-M365GroupsFromCSV which takes one parameter: $csvPath. This parameter should be the path to the CSV file that contains the data for the groups you want to create.

PowerShell function Import-M365GroupsFromCSV with string parameter $csvPath.

What the CSV File Should Contain?

Your CSV file should at least contain the following Microsoft 365 Group related columns: DisplayName, MailNickname, and Description.

CSV file with headers DisplayName, MailNickName, Description for importing Microsoft 365 groups.

Read the CSV File

Inside the function, the first operation is to read the CSV file specified by $csvPath. The Import-Csv cmdlet is used here, which reads CSV files and converts them into custom objects where each column header becomes a property of the object.

Using Import-Csv cmdlet to read the contents of CSV file.

Loop Through Each Group

This loop iterates through each object (group) in $groupList. For each group, the script attempts to create a new group in Microsoft 365 using the properties extracted from the CSV.

Looping through the groups retrieved from CSV file.

Create the Microsoft 365 Group

This is the core part of the script where the New-MgGroup cmdlet is used to create a new group. You need to pass the following parameters to New-MgGroup cmdlet. DisplayName, MailNickName and Description are extracted from the CSV file.

  • DisplayName: The display name of the group.
  • MailNickname: A required nickname for the group email (must be unique).
  • MailEnabled: Set to $true to make the group mail-enabled.
  • SecurityEnabled: Set to $false as this is a standard setting for a typical Microsoft 365 Group, which is not security-focused.
  • Description: A brief description of the group.
  • GroupTypes: Specifies the type of group; "Unified" indicates a Microsoft 365 Group.
Executing New-MgGroup cmdlet for creating Microsoft 365 groups for CSV data.

Error Handling

In case any error occurs while attempting to create a group, the catch block catches the error and displays an error message indicating which group caused the issue and what the error was.

Handling error while creating M365 groups using catch block within PowerShell function.

Step 4: Call the function to import groups from CSV file

Finally, outside the function, specify the path to the CSV file containing your group data and call the function Import-M365GroupsFromCSV with the specified CSV path.

Calling the PowerShell function and passing in the CSV file path name to $csvFilePath PowerShell variable.

Full Script for Your Reference

Here's the entire script for your reference:

Graph PowerShell Script for importing Microsoft 365 groups from CSV file.

Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex