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.
Run the below command to install Microsoft Graph PowerShell module.
Connect to Microsoft Graph using Group.ReadWrite.All permission required to read and write Microsoft 365 groups in the organization.
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.
Your CSV file should at least contain the following Microsoft 365 Group related columns: DisplayName, MailNickname, and Description.
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.
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.
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.
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.
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.
Here's the entire script for your reference:
© m365corner.com. All Rights Reserved. Design by HTML Codex