Govern Orphaned Teams with PowerShell

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.

For identifying a team without or with just only one owner is possible via Teams Admin center, but when have hundreds or thousands of teams, it is just not possible.

I wrote a short PowerShell script utilizing Teams PowerShell Module for identifying those teams. If you don’t have Teams PowerShell Module, the easiest way it install module directly from PowerShell just by running:

Install-Module -Name MicrosoftTeams

Script goes through all teams in a Office 365 tenant and finally return GridView with team information, number of owners, members and guests and email of the owner, if there is just only one.

Connect-MicrosoftTeams

$availableTeams = Get-Team
$teams = @()
foreach($team in $availableTeams)
{
 
    Write-host "Handling team: " -NoNewline -ForegroundColor Yellow
    Write-host $team.DisplayName -ForegroundColor Yellow
    $users = Get-TeamUser -GroupId $team.GroupId
    $owners = @($users | Where-Object {$_.Role -eq "owner"})
    $members = @($users | Where-Object {$_.Role -eq "member"}).Length
    $guests = @($users | Where-Object {$_.Role -eq "guest"}).Length

    $teamObject = New-Object -TypeName PSObject
    $teamObject | Add-Member -MemberType NoteProperty -Name DisplayName -Value $team.DisplayName
    $teamObject | Add-Member -MemberType NoteProperty -Name GroupID -Value $team.GroupId
    $teamObject | Add-Member -MemberType NoteProperty -Name Alias -Value $team.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].User
    }

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

$teams | Out-GridView 
Disconnect-MicrosoftTeams

Feel free to use it and modify it to your governance needs.

You can also download the script from my GitHub : List-OrphanedTeams.ps1

One thought on “Govern Orphaned Teams with PowerShell

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: