I recently needed to get a list of users that belong to a specific Microsoft Teams team - and there isnt anything out of the box to get this using the Teams app. AFAIK, the only way to do this is using the Microsoft graph API - for which there are a few options.
For something quick (e.g. getting a list of users in a team), using the Graph explorer
could be easy enough. On the other hand, if you need something more robust, you should program against the (REST) API.
Navigate to Graph explorer
, sign in and authenticate yourself against the specific O365 tenant you are interested in - most folks would only have one.
Once authenticated, on the panel on the left you see several sample queries and scroll down until you see the Teams.
To get members of a specific team, you need to get the team ID for that Team. This is unique ID (GUID) and doesn’t change over the lifetime of the team. If you have this, then go ahead to the next section - Getting team members.
On the query panel in Graph explorer, select the “my joined teams” and run the query. You will get a JSON back that contains the list of teams that you are a member of. The “id” element represents the Team ID which you would need for any team related API calls. For example, I am interested in this specific #AI team: “#Reinforcement Learning and Decision AI”.
Once you have the Team ID (the unique GUID that each identifies each team), you can get the members of the team using that option on the left. As shown on the screenshot below, you do need to pass in the team ID to the REST API and this would be something like this (and don’t worry what I am showing below is a fictious GUID):
If you want something more robust and repeatable, then using the API (via code) or PowerShell might be better. If you are programming, you will need to register an app - which can authenticate
using the Identify platform
. This of course is quite powerful, but at times for simple things might be a bit too much.
In my simple task to get users from Teams, I prefer the PowerShell option. To get this going first you need to install the MicrosoftTeam module. This can be done using the command below.
Depending on your configuration you might get a warning as shown below.
Once the Teams PowerShell module is installed, you can run PowerShell scripts against Teams and achieve the same result. I have two scripts below showing the same steps as with the Graph Explorer above. One of these gets details of the teams that a user is a member of. And the second script is to get members of a selected team.
The PowerShell script below to get a Team details is below; you can also get it from GitHub
. Before you run this, there are two variables that need to be set.
One, the path where you want the team details to be exported (this is a csv file).
Two, set the email that you will use. This needs to be the same one that you authenticated against.
You will be prompted to sign in to authentic and this should be an experience that most folks would be familiar with. Note, each time you run the script, you need to authenticate - and this is irrespective of say if you are already logged into Teams of Office 365.
Assuming you have authenticated successfully, you should see an output like the one shown below; and a csv file in the path you configured will be created. This file will always be overwritten - without any prompts (of course this is assuming no other process is open that has a lock on that file).
#Set these variables, to what makes sense in your situation. The email here is the one that is the one connected to your teams account.$exportLocation="C:\temp\team-details.csv"$emailAddress="[email protected]"#Authenticate against teamsConnect-MicrosoftTeams#PatienceWrite-Host-ForegroundColorBlue"Successfully connected to Teams"Write-Host-ForegroundColorBlue"Getting all team details for user: $($emailAddress)"Write-Host-ForegroundColorBlue"Please be patient, if there are a lot of teams, this can take a while..."# Get all of the team Groups IDs# $GetUsersTeams = (Get-Team).GroupID$GetUsersTeams=Get-Team-User$emailAddress$Report=@()# Will hold a basic count of user types and teams$unavailableTeamCount=0# Loop through all teams that the user belongs to$currentIndex=1ForEach($thisTeamin$GetUsersTeams){# Show some output to the userWrite-Progress-Id0-Activity"Building report from Microsoft Teams"-Status"$currentIndex of $($GetUsersTeams.Count)"-PercentComplete(($currentIndex/$GetUsersTeams.Count)*100)# Attempt to get team details, throw error message if no accesstry{# Get team members#$users = Get-TeamUser -GroupId $thisTeam.groupID# Create an object to hold all values$teamReportObject=New-ObjectPSObject-Property@{GroupID=$thisTeam.GroupIDTeamName=$thisTeam.DisplayNameDescription=$thisTeam.DescriptionArchived=$thisTeam.ArchivedVisibility=$thisTeam.VisibilityeMail=$thisTeam.MailNickName}# Add to the report$Report+=$teamReportObject}catch[Microsoft.TeamsCmdlets.PowerShell.Custom.ErrorHandling.ApiException]{Write-Host-ForegroundColorYellow"No access to $($team.DisplayName) team, cannot generate report"$unavailableTeamCount++}$currentIndex++}Write-Progress-Id0-Activity" "-Status" "-Completed# Disconnect from teamsDisconnect-MicrosoftTeams# Provide some nice outputWrite-Host-ForegroundColorGreen"============================================================"Write-Host-ForegroundColorGreen" Microsoft Teams User Report "Write-Host-ForegroundColorGreen""Write-Host-ForegroundColorGreen" Count of All Teams - $($GetUsersTeams.Count) "Write-Host-ForegroundColorGreen" Count of Inaccesible Teams - $($unavailableTeamCount) "Write-Host-ForegroundColorGreen""$Report|Export-CSV$exportLocation-NoTypeInformation-ForceWrite-Host-ForegroundColorBlue"Exported report to $($exportLocation)"
Now that you have the Team ID you are interested, you can run the other PowerShell script (also available on GitHub
) to get a list of all the users in a specific team. Like the previous script, you would need set a couple of variables in the script:
The Team ID for the team you are interested in.
Path for the csv file with details to be saved.
Once you have authenticated and ran the script, the output will look like the one shown below. You get a summary of the team details, and details of the Teams users and their type (owner, member, or guest). And just like earlier, the file will be overwritten without a prompt, assuming no locks on it.
#Global variables to set:#path of the file where to export#specific ID of the team that you want the users for. $exportLocation="C:\temp\RL-decision-AI-export.csv"$TEAM_ID="f3f9ad1f-beea-4026-9b86-dd3788404999"$Report=@()# counters$ownerCount=0$memberCount=0$guestCount=0#connect to teamsConnect-MicrosoftTeams$team=Get-Team-GroupId$TEAM_ID#Patience, supposed to be a virtueWrite-Host-ForegroundColorBlue"Successfully connected to Team: $($team.DisplayName)"Write-Host-ForegroundColorBlue"Getting all users in the team"Write-Host-ForegroundColorBlue"Please be patient, if there are a lot of users, this can take a while..."# Attempt to get team users, throw error message if no accesstry{# Get team members$users=Get-TeamUser-GroupId$team.groupID# Loop through and get all the users$currentIndex=1# foreach user create a line in the reportForEach($userin$users){# Show some output to the userWrite-Progress-Id0-Activity"Generating user report from Teams"-Status"$currentIndex of $($users.Count)"-PercentComplete(($currentIndex/$users.Count)*100)# Maintain a count of user typesswitch($user.Role){"owner"{$ownerCount++}"member"{$memberCount++}"guest"{$guestCount++}}# Create an object to hold all values$ReportObject=New-ObjectPSObject-Property@{User=$user.NameEmail=$user.UserRole=$user.Role}# Add to the report$Report+=$ReportObject$currentIndex++}}catch[Microsoft.TeamsCmdlets.PowerShell.Custom.ErrorHandling.ApiException]{Write-Host-ForegroundColorYellow"No access to $($team.DisplayName) team, cannot generate report"}#Complete progressWrite-Progress-Id0-Activity" "-Status" "-Completed# Disconnect from teamsDisconnect-MicrosoftTeams# Write out details for the userWrite-Host-ForegroundColorGreen"============================================================"Write-Host-ForegroundColorGreen" Microsoft Teams User Report "Write-Host-ForegroundColorGreen""Write-Host-ForegroundColorGreen"Team Details:"Write-Host-ForegroundColorGreen"Name: $($team.DisplayName)"Write-Host-ForegroundColorGreen"Description: $($team.Description)"Write-Host-ForegroundColorGreen"Mail Nickname: $($team.MailNickName)"Write-Host-ForegroundColorGreen"Archived: $($team.Archived)"Write-Host-ForegroundColorGreen"Visiblity: $($team.Visibility)"Write-Host-ForegroundColorGreen""Write-Host-ForegroundColorGreen"Team User Details:"Write-Host-ForegroundColorGreen"Owners - $($ownerCount)"Write-Host-ForegroundColorGreen"Members - $($memberCount)"Write-Host-ForegroundColorGreen"Guests - $($guestCount)"Write-Host-ForegroundColorGreen"============================================================"$Report|Export-CSV$exportLocation-NoTypeInformation-ForceWrite-Host-ForegroundColorBlue"Exported report to $($exportLocation)"
Of course, programming against the API is always more powerful, but sometimes quick and easy is what is needed. :)