Using Get-MgTeamMember with Get-MgUser: Fetch Personal Details of Team Members
Managing team memberships in Microsoft Teams is crucial for collaboration and access control. The Get-MgTeamMember
cmdlet retrieves the members of a specified team, but it only provides their Team Member IDs by default. To fetch additional details such as User ID, Display Name, User Principal Name (UPN), and Email, you need to pair it with the Get-MgUser
cmdlet. This article demonstrates how to use these cmdlets together to efficiently retrieve detailed team member information, with a special emphasis on accessing the AdditionalProperties
object.
Usage Example
# Specify the Team ID
$teamId = "9f47ec97-db44-41db-8867-3793cff0a49a" # Replace with your Team ID
# Retrieve all team members
$teamMembers = Get-MgTeamMember -TeamId $teamId -All
# Loop through each team member to fetch personal details
foreach ($member in $teamMembers) {
$additionalProps = $member.AdditionalProperties
$userId = $additionalProps["userId"]
if ($userId -ne $null -and $userId -ne "") {
Get-MgUser -UserId $userId | Select-Object Id, DisplayName, UserPrincipalName
}
}
Accessing AdditionalProperties for User Details
When using the Get-MgTeamMember
cmdlet, the User ID for team members is not directly accessible. Instead, it is nested within the AdditionalProperties
object. To retrieve a team member's details, you must extract the userId
property from AdditionalProperties
and pass it to the Get-MgUser
cmdlet.
# Access the userId from AdditionalProperties
$additionalProps = $member.AdditionalProperties
$userId = $additionalProps["userId"]
# Use the userId to fetch personal details
if ($userId -ne $null -and $userId -ne "") {
Get-MgUser -UserId $userId | Select-Object Id, DisplayName, UserPrincipalName
}
Tips and Best Practices
- Always Access AdditionalProperties: Since
Get-MgTeamMember
does not directly expose the User ID, you must extract it from the AdditionalProperties
object.
- Use Select-Object for Streamlined Output: Include only the necessary fields, such as
Id
, DisplayName
, and UserPrincipalName
, to make the output concise and relevant.
- Export the Results: Save the retrieved data to a CSV file for documentation or analysis:
$results = foreach ($member in $teamMembers) {
$additionalProps = $member.AdditionalProperties
$userId = $additionalProps["userId"]
if ($userId -ne $null -and $userId -ne "") {
Get-MgUser -UserId $userId | Select-Object Id, DisplayName, UserPrincipalName
}
}
$results | Export-Csv -Path "TeamMembersDetails.csv" -NoTypeInformation
- Optimize for Large Teams: If the team has many members, process the data in smaller batches to avoid throttling.
Possible Errors & Solutions
Error |
Cause |
Solution |
The specified object was not found in the directory. |
The Team ID is invalid or does not exist in the directory. |
Verify the Team ID using the Get-MgTeam cmdlet:
Get-MgTeam -Filter "displayName eq 'Team Name'"
|
Insufficient privileges to complete the operation. |
Missing permissions like TeamMember.Read.All or User.Read.All . |
Ensure the account running the script has these permissions. |
No members found for the team. |
The team does not have any assigned members. |
Confirm the team's membership in Microsoft Teams. |
Request throttled due to too many API calls. |
Too many requests being sent to the API. |
Add a delay between requests or process members in batches. |
Use Cases
- Generate Detailed Team Membership Reports: Create reports showing team members’ Display Names, UPNs, and Emails for internal documentation or sharing with stakeholders.
- Audit Team Membership: Verify that only authorized users are members of sensitive teams, ensuring compliance with organizational policies.
- Enhance Collaboration Management: Retrieve team member details to manage access permissions or send targeted communications.
- Support Compliance Efforts: Export membership details for compliance reviews or audits.
Conclusion
Pairing Get-MgTeamMember
with Get-MgUser
is essential for retrieving detailed information about team members in Microsoft Teams. Since Get-MgTeamMember
provides only the Team Member IDs, you must access the AdditionalProperties
object to extract the userId
and use it with Get-MgUser
. This approach simplifies team management, supports compliance reporting, and ensures accurate insights into team memberships. Implement this method today to streamline your Teams administration tasks!