Skip to content

Instantly share code, notes, and snippets.

@plamber
Last active March 20, 2021 08:18
Show Gist options
  • Select an option

  • Save plamber/c2a2f4ee4d286dddd343d78db36e6734 to your computer and use it in GitHub Desktop.

Select an option

Save plamber/c2a2f4ee4d286dddd343d78db36e6734 to your computer and use it in GitHub Desktop.
List all Microsoft Teams team's Owners and Members
$fileExportPath = "<PUTYOURPATHHERE.csv>"
# process teams that you have joined only
$joined = $false
$m365Status = m365 status
if ($m365Status -eq "Logged Out") {
# Connection to Microsoft 365
m365 login
}
# configure the CLI to output JSON on each execution
m365 cli config set --key output --value json
$exportData = @()
$teams = m365 teams team list --joined $joined | ConvertFrom-Json
# you can use the next line if you already know the GroupId/TeamId
#$teams = @(m365 teams team get --id $teamId | ConvertFrom-Json)
$i = 0
$teams | ForEach-Object {
$team = $_
$i++
Write-Host "Processing Team '$($team.displayName)' - ($i/$($teams.length))"
$owners = $null
$owners = m365 teams user list --teamId $team.id --role Owner --query "[].userPrincipalName" | ConvertFrom-Json
$members = $null
$members = m365 teams user list --teamId $team.id --role Member --query "[].userPrincipalName" | ConvertFrom-Json
$exportData += [PSCustomObject]@{ Id = $team.id; DisplayName = $team.displayName; Owners = $owners -join ', '; Members = $members -join ', '}
}
Write-Host "Exporting file to $fileExportPath..."
$results | Export-Csv -Path $fileExportPath -NoTypeInformation
Write-Host "Completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment