Skip to main content

Delete all Microsoft 365 groups

Author: Laura Kokkarinen

There are so many different ways to create Microsoft 365 groups. Teams, Planner, SharePoint team sites, etc. — you can accumulate a lot of them very fast. Use this script below to delete the ones you no longer need.

$sparksjoy = "All Company", "TEMPLATE Project", "We have cats in this team! Join!"
$groups = m365 entra m365group list -o json | ConvertFrom-Json
$groups = $groups | where {-not ($sparksjoy -contains $_.displayName)}
if ($groups.Count -eq 0) { break }
$groups | Format-Table displayName
Write-Host "Total:" $groups.Count
Read-Host -Prompt "Press Enter to start deleting (CTRL + C to exit)"
$progress = 0
$total = $groups.Count
foreach ($group in $groups)
{
$progress++
Write-Host $progress / $total":" $group.displayName
m365 entra m365group remove --id $group.id --force
}
CTRL + M