test.ps1

# Get all teams in the tenant
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -Property Id, DisplayName -All

# Create an array to store the results
$results = @()

# Loop through each team
foreach ($team in $teams) {
    $teamId = $team.Id
    $teamName = $team.DisplayName
    $channels = Get-MgTeamChannel -TeamId $teamId -All

    foreach ($channel in $channels) {
        $channelName = $channel.DisplayName

        # Get members og channel
        $members = Get-MgTeamChannelMember -TeamId $teamId -ChannelId $channel.Id -All

        foreach ($member in $members) {
            $memberName = $member.DisplayName
            $memberMail = $member.AdditionalProperties.email

            # Store the result in a custom object
            $results += [PSCustomObject]@{
                TeamName   = $teamName
                ChannelName = $channelName
                MemberName = $memberName
                MemberMail = $memberMail
            }
        }
    }
}

# Output the results in a table format
$results | Format-Table -AutoSize

# Optionally export the results to a CSV file
$results | Export-Csv -Path "TeamsMembersList.csv" -NoTypeInformation