Entra / Microsoft 365 · Teams
Find Teams memberships for users
Use Graph to generate a report of Teams memberships for users in a specified group.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -NoWelcome -Scopes Group.Read.All, Groups.Read.All, Team.ReadBasic.All
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Connect-MgGraph -NoWelcome -Scopes Group.Read.All, Groups.Read.All, Team.ReadBasic.All# The group that defines the users to report on. Make sure that you change this for your tenant.$GroupId = (Get-MgGroup -Filter "displayName eq 'Users to Monitor for Teams Membership'").Id# If you want to process all users, make the change here to fetch all licensed users (accounts with assigned Teams licenses)[array]$Users = Get-MgGroupMember -GroupId $GroupId$Report = [System.Collections.Generic.List[Object]]::new()ForEach ($User in $Users) {Write-Host ("Fetching details of Teams membership for user {0} ({1}):" -f $User.additionalProperties.displayName, $User.additionalProperties.userPrincipalName) -ForegroundColor YellowTry {[array]$Memberships = Get-MgUserJoinedTeam -UserId $User.Id -ErrorAction Stop| Sort-Object DisplayName} Catch {Write-Host ("Error fetching Teams memberships for {0}. Moving to next user" -f $User.additionalProperties.displayName) -ForegroundColor RedContinue}If ($Memberships.Count -gt 0) {Write-Host ("Found {0} Teams group memberships for {1}." -f $Memberships.Count, $User.additionalProperties.displayName) -ForegroundColor GreenForEach ($Membership in $Memberships) {$ReportLine = [PSCustomObject][Ordered]@{UserId = $User.IdUser = $User.additionalProperties.displayNameUPN = $User.additionalProperties.userPrincipalName'Job Title' = $User.additionalProperties.jobTitle'Office' = $User.additionalProperties.officeLocation'Team' = $Membership.DisplayName'TeamId' = $Membership.Id'Timestamp' = Get-Date -format 'dd-MMM-yyyy HH:mm:ss'}$Report.Add($ReportLine)}} Else {Write-Host ("No Teams group memberships found for {0}." -f $User.additionalProperties.displayName) -ForegroundColor Yellow}}Write-Host ""# Generate the report in either Excel worksheet or CSV format, depending on if the ImportExcel module is availableIf (Get-Module ImportExcel -ListAvailable) {$ExcelGenerated = $TrueImport-Module ImportExcel -ErrorAction SilentlyContinue$ExcelOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Teams Memberships.xlsx"$Report | Export-Excel -Path $ExcelOutputFile -WorksheetName "Teams Memberships" -Title ("Teams Memberships {0}" -f (Get-Date -format 'dd-MMM-yyyy')) -TitleBold -TableName "TeamsMemberships"} Else {$CSVOutputFile = ((New-Object -ComObject Shell.Application).Namespace('shell:Downloads').Self.Path) + "\Teams Memberships.CSV"$Report | Export-Csv -Path $CSVOutputFile -NoTypeInformation -Encoding Utf8}If ($ExcelGenerated -eq $true) {Write-Host ("Teams memberships report is available in Excel workbook {0}" -f $ExcelOutputFile)} Else {Write-Host ("Teams memberships report is available in CSV file {0}" -f $CSVOutputFile)}
Attribution
Author
Office365itpros