Identifying Duplicate Microsoft 365 Group Names Using PowerShell
Introduction
It is possible to create M365 Groups and Teams with the same name, and there is currently no built-in way to prevent this. Having duplicate names can cause confusion and increase risks, including:
- Wrongly Granting Permissions: Users may accidentally grant permissions to the wrong M365 Group or Team, leading to unauthorized access.
- Storing Content in the Wrong Location: Duplicate names can lead to users storing content in the incorrect group or team, making it difficult to find and manage information.
- Communication Confusion: Team members may get confused about which group or team to use for specific tasks, leading to miscommunication and inefficiency.
- Compliance Issues: Duplicate names can complicate compliance and auditing processes, making it harder to track and manage group activities.
To mitigate these risks, you can use PowerShell to check for duplicate names and ensure that each M365 Group and Team has a unique name. Also consider limiting self-service controlling who can create a Teams or M365 group and use a provisioning tool to check if the name is already being used to prevent creation.
PowerShell Script
The following PowerShell script helps identify and prevent the creation of duplicate M365 Group and Teams names.
param (
[Parameter(Mandatory = $true)]
[string] $domain
)
Clear-Host
$dateTime = (Get-Date).toString("dd-MM-yyyy-hh-ss")
$invocation = (Get-Variable MyInvocation).Value
$directorypath = (Split-Path $invocation.MyCommand.Path) + "\"
$exportFilePath = Join-Path -Path $directorypath -ChildPath $([string]::Concat($domain,"-duplicateM365_",$dateTime,".csv"));
$adminSiteURL = "https://$domain-Admin.SharePoint.com"
Connect-PnPOnline -Url $adminSiteURL
# Retrieve all M365 groups
$groups = get-PnPMicrosoft365Group
# Find duplicate group names
$duplicateGroups = $groups | Group-Object DisplayName | Where-Object { $_.Count -gt 1 }
# Create a report
$report = @()
foreach ($group in $duplicateGroups) {
foreach ($item in $group.Group) {
$report += [PSCustomObject]@{
DisplayName = $item.DisplayName
GroupId = $item.Id
Mail = $item.Mail
}
}
}
# Export the report to a CSV file
$report | Export-Csv -Path $exportFilePath -NoTypeInformation
Disconnect-PnPOnline
Conclusion
This script can help to identify duplicate M365 Group to ensure each group has a unique name reducing confusion for end users.