Skip to main content

Govern orphaned Microsoft Teams

Author: Matti Paukkonen

Every team needs an owner, at least one. Common best practice is that you should have at least two users in owner role. Teams is not allowing the last owner to leave the team, but there might occasions when last owner is removed, example when people are leaving the organization and account gets deleted. This script finds those teams that no longer have an owner.

$availableTeams = m365 teams team list -o json | ConvertFrom-Json
$teams = @()
foreach ($team in $availableTeams) {

Write-host "Handling team: " -NoNewline -ForegroundColor Yellow
Write-host $team.DisplayName -ForegroundColor Yellow
$group = m365 entra m365group get --id $team.id -o json | ConvertFrom-Json
$users = m365 teams user list --teamId $team.id -o json | ConvertFrom-Json
$owners = @($users | Where-Object { $_.userType -eq "Owner" })
$members = @($users | Where-Object { $_.userType -eq "Member" }).Length
$guests = @($users | Where-Object { $_.userType -eq "Guest" }).Length

$teamObject = New-Object -TypeName PSObject
$teamObject | Add-Member -MemberType NoteProperty -Name DisplayName -Value $team.displayName
$teamObject | Add-Member -MemberType NoteProperty -Name Alias -Value $group.mailNickName
$teamObject | Add-Member -MemberType NoteProperty -Name "Number of Owners" -Value $owners.Length
$teamObject | Add-Member -MemberType NoteProperty -Name "Number of Members" -Value $members
$teamObject | Add-Member -MemberType NoteProperty -Name "Number of Guests" -Value $guests
if ($owners.Count -eq 1) {
$teamObject | Add-Member -MemberType NoteProperty -Name "Owner" -Value $owners[0].displayName
}

write-host " ...Done" -ForegroundColor Green
$teams += $teamObject
}

$teams | Format-Table -AutoSize
CTRL + M