GitHub Icon Image

List all team members in Microsoft Teams teams in the tenant

List all team members in Microsoft Teams teams in the tenant and exports the results in a CSV.

  • CLI for Microsoft 365 with PowerShell
function Get-TeamMembers(
    [Parameter(Mandatory = $false)][string] $teamID
) {
    if (!$teamID) {
        Write-Error "'Team ID' is required!"
        return
    }
    Write-Host "Retrieving the users.."
    $results = @()
    $users = m365 teams user list --teamId $teamID -o 'json' | ConvertFrom-Json
    if ($users.length -gt 0) {
        foreach ($user in $users) {
            $results += [pscustomobject][ordered]@{
                ID             = $user.id
                "Display Name" = $user.displayName
                UPN            = $user.userPrincipalName
                Role           = $user.userType
            }
        }
    }
    else {
        Write-Output "No team members!"
    }
    Write-Host "Exporting the results.."
    $results | Export-Csv -Path "TeamMembers.csv" -NoTypeInformation
    Write-Host "Completed."
}

Write-Host "Ensure logged in"
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    Write-Host "Logging in the User!"
    m365 login --authType browser
}

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Source Credit

Sample first appeared on List all team members in Microsoft Teams teams in the tenant | CLI for Microsoft 365

Contributors

Author(s)
Sudharsan Kesavanarayanan

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI