List Team Members Across Microsoft Teams Using PowerShell

Managing and maintaining Microsoft Teams across a large tenant can be a challenging task, especially when you need to keep track of all the members within various teams. With Microsoft Graph PowerShell, you can automate the process of retrieving team members, saving you time and effort. In this article, we’ll explore a script that allows you to list all team members across Microsoft Teams in your tenant, including the team name, member name, and member email in a tabular format.


The Script

# Import the Microsoft Graph PowerShell module
Import-Module Microsoft.Graph.Teams

# Authenticate to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All"

# 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

    # Get the members of the team
    $members = Get-MgGroupMember -GroupId $teamId -All

    # Loop through each member and add the result to the array
    foreach ($member in $members) {
        # Retrieve user details only if the member is a user
        $userDetails = Get-MgUser -UserId $member.Id -Property DisplayName, UserPrincipalName

        $memberName = $userDetails.DisplayName
        $memberMail = $userDetails.UserPrincipalName

        # Store the result in a custom object
        $results += [PSCustomObject]@{
            TeamName   = $teamName
            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

How the Script Works

This script leverages Microsoft Graph PowerShell to automate the task of retrieving team members from all Microsoft Teams within a tenant. Below is an explanation of how it works:

  • Module Import and Authentication: The script starts by importing the Microsoft.Graph.Teams module and authenticating the session with the required permissions (Group.Read.All).
  • Fetching All Teams: It then retrieves all teams within the tenant using the Get-MgGroup cmdlet, filtering for groups that have the 'Team' resource provisioning option.
  • Looping Through Teams and Members: For each team, the script retrieves all members using the Get-MgGroupMember cmdlet and fetches detailed information (e.g., name and email) for each member using the Get-MgUser cmdlet.
  • Storing and Displaying Results: The results, including the team name, member name, and member email, are stored in a custom object and displayed in a table format. The results can also be exported to a CSV file for further analysis.

Further Enhancing the Script

This script provides a solid foundation for managing team members in Microsoft Teams, but you can enhance it in several ways:

  • Filtering Specific Teams or Members: Modify the script to filter specific teams or members based on certain criteria, such as department, region, or role.
  • Including Additional Member Information: Expand the script to include more detailed information about each member, such as their role within the team, department, or job title.
  • Automating Notifications: Integrate the script with email notifications to automatically inform administrators when new members are added to teams.

Possible Errors & Solutions

"Authentication Issues"

Error: You might encounter issues when authenticating with Microsoft Graph.

Solution: Ensure that you have the correct permissions (Group.Read.All) and that you are using an account with sufficient privileges.

"Empty or Missing Data"

Error: The script might return empty or missing data for certain members.

Solution: Ensure that the Get-MgUser cmdlet is correctly retrieving user details. Double-check that the members you are querying are actual users and not other types of objects like groups or devices.

"Throttling Limits"

Error: Running this script in a large tenant may hit Microsoft Graph API throttling limits.

Solution: Implement a delay between API calls or use pagination to handle large datasets efficiently.


Conclusion

This PowerShell script provides a powerful way to manage and monitor team members across your Microsoft Teams environment. By automating the process of retrieving member details, you can streamline your administrative tasks and ensure that you have up-to-date information on who is part of each team. With the ability to customize and enhance the script further, it can become an invaluable tool in your Microsoft 365 management toolkit.


© m365corner.com. All Rights Reserved. Design by HTML Codex