Creating Microsoft 365 Groups manually can be a time-consuming process, especially when dealing with a large number of groups. This script automates the process, saving time and reducing the risk of errors.

In this blog post, we’ll explore a PowerShell script that automates the creation of Microsoft 365 Groups. This script is particularly useful when you need to create multiple groups with different configurations.

Microsoft 365 Groups Overview

Microsoft 365 Groups is a service that works with Microsoft 365 tools to provide a platform for collaboration. Each group comes with a shared mailbox, calendar, SharePoint site, and can also be connected with other Microsoft services like Planner and Power BI.

Groups can be public (anyone in your organization can join) or private (only invited users can join), and they help to streamline collaboration by providing a single set of permissions across Microsoft 365 services.

PowerShell Script to create Microsoft 365 Groups

Here’s the script we’ll be discussing:

Set-SPOUser -site $URL -LoginName $SecondaryAdmin -IsSiteCollectionAdmin $TrueConnect-PnPOnline -Url "https://MsTalk-admin.sharepoint.com/" -UseWebLogin 
Connect-PnPOnline –Graph 
$path = "D:\\createGroups.csv"

function StartGroupCreation()
{
    $AllGroups = Import-Csv -Path $path 

    foreach ($group in $AllGroups) {
        $Groupname = $group.Name
        $Owners =  $group.Owners
        $Members = $group.Members
        $Groupalias = $group.Email
        $Groupprivacy = $group.Privacy
       
        CreateMicrosoft365Group -Groupname $Groupname -Groupalias $Groupalias -Owners $Owners -Members $Members -Groupprivacy $Groupprivacy
    }
}

function CreateMicrosoft365Group($Groupname, $Groupalias, $Owners, $Members,  $Groupprivacy)
{   
    $arrOwners = $Owners.split(',')
    $arrMembers = $Members.split(',')
   
    Write-Host 'Started creation of M365 Group: ' + $Groupname
    if ($Groupprivacy -eq 'Private')
    {
        $NewGroup = New-PnPMicrosoft365Group -DisplayName $Groupname -MailNickname $Groupalias -Description $Groupname -Owners $arrOwners -Members $arrMembers -IsPrivate
    }
    else
    {
        $NewGroup = New-PnPMicrosoft365Group -DisplayName $Groupname -MailNickname $Groupalias -Description $Groupname -Owners $arrOwners -Members $arrMembers 
    }
    Write-Host 'Created M365 Group: ' + $Groupname -ForegroundColor Green    
}

This PowerShell script is used to automate the creation of Microsoft 365 Groups using the SharePoint PnP PowerShell module. Here’s a detailed breakdown of the script:

  1. Connect-PnPOnline -Url "https://MsTalk-admin.sharepoint.com/" -UseWebLogin: This line connects to the SharePoint Online admin center using web login.
  2. Connect-PnPOnline –Graph: This line connects to the Microsoft Graph, which is the gateway to data and intelligence in Microsoft 365.
  3. $path = "D:\\createGroups.csv": This line sets the path to a CSV file that contains the details of the groups to be created.
  4. StartGroupCreation(): This function reads the CSV file and for each row (representing a group), it calls the CreateMicrosoft365Group function to create the group.
  5. CreateMicrosoft365Group($Groupname, $Groupalias, $Owners, $Members, $Groupprivacy): This function creates a Microsoft 365 group with the provided details. It uses the New-PnPMicrosoft365Group cmdlet to create the group. If the group privacy is set to ‘Private’, it creates a private group; otherwise, it creates a public group.

By using this script, administrators can ensure that all specified groups are created with the correct settings. This is particularly useful in large organizations where there may be a need to create multiple groups with different configurations.

Adnan, a distinguished professional, boasts an impressive track record as a Microsoft MVP, having achieved this prestigious recognition for the eighth consecutive year since 2015. With an extensive career spanning over 18 years, Adnan has honed his expertise in various domains, notably excelling in SharePoint, Microsoft 365, Microsoft Teams, the .Net Platform, and Microsoft BI. Presently, he holds the esteemed position of Senior Microsoft Consultant at Olive + Goose. Notably, Adnan served as the MCT Regional Lead for the Pakistan Chapter from 2012 to 2017, showcasing his leadership and commitment to fostering growth within the tech community. His journey in the realm of SharePoint spans 14 years, during which he has undertaken diverse projects involving both intranet and internet solutions for both private and government sectors. His impact has transcended geographical boundaries, leaving a mark on projects in the United States and the Gulf region, often collaborating with Fortune 500 companies. Beyond his roles, Adnan is a dedicated educator, sharing his insights and knowledge as a trainer. He also passionately advocates for technology, frequently engaging with the community through speaking engagements in various forums. His multifaceted contributions exemplify his dedication to the tech field and his role in driving its evolution.

Leave a Reply