
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:
Connect-PnPOnline -Url "https://MsTalk-admin.sharepoint.com/" -UseWebLogin: This line connects to the SharePoint Online admin center using web login.Connect-PnPOnline –Graph: This line connects to the Microsoft Graph, which is the gateway to data and intelligence in Microsoft 365.$path = "D:\\createGroups.csv": This line sets the path to a CSV file that contains the details of the groups to be created.StartGroupCreation(): This function reads the CSV file and for each row (representing a group), it calls theCreateMicrosoft365Groupfunction to create the group.CreateMicrosoft365Group($Groupname, $Groupalias, $Owners, $Members, $Groupprivacy): This function creates a Microsoft 365 group with the provided details. It uses theNew-PnPMicrosoft365Groupcmdlet 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.






No Comments