List ownerless teams
Easily identify ownerless teams in your tenant
Summary
If your organization has been using Microsoft Teams for more that a few years, you'll no doubt have a number of Teams that have been orphaned, probably because the original owner has moved on. This script will list all Teams that have fewer than a specified number of owners, and export them to a CSV file.
Connect-PnPOnline -ClientId "GUID" -Tenant "GUID" -CertificatePath -CertificatePassword "Password"
$MinimumRequiredOwners = 1;
$Groups = Get-PnPMicrosoft365Group -IncludeOwners | Where-Object {$_.Owners.Count -le $MinimumRequiredOwners -and $_.HasTeam}
$MappedObjects = [System.Collections.ArrayList]@()
foreach ($Group in $Groups) {
$MappedObject = [PSCustomObject]@{
GroupId = $Group.GroupId
DisplayName = $Group.DisplayName
OwnersCount = $Group.Owners.Count
Owners = $Group.Owners | Select-Object -Property "Email" | Join-String -Property "Email" -Separator "; "
}
$MappedObjects += $MappedObject
}
$fileName = "$(Get-Date -Format ("yyyy-MM-dd"))-GroupsWithFewerThan$($MinimumRequiredOwners)Owners.csv";
$MappedObjects | Select-Object -Property * | Export-Csv -Path ".\$fileName" -Encoding UTF8 -Delimiter ";" -Force;
$minimumRequiredOwners = 1
m365 login --ensure
$teams = @(m365 teams team list --output json | ConvertFrom-Json)
$mappedObjects = foreach ($team in $teams) {
# Only the owners are needed, and the mail address is not returned unless it is asked for.
# The CLI's Entra app needs User.ReadBasic.All besides GroupMember.Read.All: without it, Graph
# still lists the owners but returns their mail as null, so the Owners column stays empty.
$ownersJson = m365 entra m365group user list --groupId $team.id --role Owner --properties "mail" --output json
if ($LASTEXITCODE -ne 0) {
Write-Warning "Could not read the owners of '$($team.displayName)', skipping it."
continue
}
$owners = @($ownersJson | ConvertFrom-Json)
if ($owners.Count -le $minimumRequiredOwners) {
[PSCustomObject]@{
GroupId = $team.id
DisplayName = $team.displayName
OwnersCount = $owners.Count
Owners = ($owners.mail | Where-Object { $_ }) -join "; "
}
}
}
$fileName = "$(Get-Date -Format "yyyy-MM-dd")-GroupsWithFewerThan$($minimumRequiredOwners)Owners.csv"
$mappedObjects | Export-Csv -Path ".\$fileName" -Encoding UTF8 -Delimiter ";" -Force
Contributors
| Author(s) |
|---|
| Dan Toft |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.