Get Team privacy and ownership settings

5/5 - (2 votes)

One of my former peers pinged me the other day asking if I had a script in my back pocket that would allow an analyst at a customer to get a list of Teams, whether they were public or private, and who the owners were.

The information can be obtained from a few different portals, but it’s generally a pain to get it all in one spot. For example, the Microsoft 365 admin center displays it all, but the download/export option doesn’t include the group owners.  So, I’ve created a quick script that will help gather that info.

In order to use it, you’ll need both the Azure AD and MicrosoftTeams PowerShell modules.  Connect to both (Connect-AzureAD and Connect-MicrosoftTeams), and then away you go!

Copy and save this as a .ps1, and run when connected to both Azure AD and Teams PowerShell modules.

param (
[switch]$IncludeMembers,
[string]$Outputfile
)

[array]$Teams = Get-Team
$Report = @()
$i = 1
$Count = $Teams.Count

# Build list of groups
foreach ($Team in $Teams)
{
Write-Progress -Activity "Gathering Teams data..." -Id 1 -PercentComplete (($i/$Count) * 100)
[array]$TeamOwners = (Get-AzureAdGroupOwner -ObjectId $Team.GroupId).UserPrincipalName
[array]$TeamMembers = (Get-AzureAdGroupMember -ObjectId $Team.GroupId).UserPrincipalName
$TeamData = New-Object -TypeName System.Management.Automation.PSObject
$TeamData | Add-Member -NotePropertyName "Team" -NotePropertyValue $Team.DisplayName
$TeamData | Add-Member -NotePropertyName "ObjectID" -NotePropertyValue $Team.GroupId
$TeamData | Add-Member -NotePropertyName "Visibility" -NotePropertyValue $Team.Visibility
$TeamData | Add-Member -NotePropertyName "Archived" -NotePropertyValue $Team.Archived
$TeamData | Add-Member -NotePropertyName "Owners" -NotePropertyValue $($TeamOwners -join ";")
if ($IncludeMembers) { $TeamData | Add-Member -NotePropertyName "Members" -NotePropertyValue $($TeamMembers -join ";") }
$Report += $TeamData
$i++
}

$Report | Export-Csv -Path $Outputfile -NoTypeInformation

Ta-da!

Let me know in the comments about any modifications you make to add features!