Identifying and Managing Empty Microsoft Teams

Managing Microsoft Teams effectively is crucial for maintaining an organized and efficient collaboration environment. Over time, teams might become inactive or even empty, leading to clutter and unnecessary resource allocation. This article presents a Graph PowerShell script designed to identify and list empty Microsoft Teams in your organization. The script outputs the Team Name, Description, and Team Type, providing a clear overview of teams that might need attention.


The Script

Below is the Graph PowerShell script that checks for empty Microsoft Teams and outputs the relevant data in a tabular format:


    # Connect to Microsoft Graph with the necessary scopes
    Connect-MgGraph -Scopes "Team.ReadBasic.All Group.Read.All"

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

    # Prepare an array to store the data of empty teams
    $emptyTeamsData = @()

    foreach ($team in $teams) {
    # Get the member count for each team
    $memberCount = (Get-MgGroupMember -GroupId $team.Id -All).Count

    # If the team is empty (no members), add its information to the array
    if ($memberCount -eq 0) {
    # Determine the team type based on visibility
    $teamType = if ($team.Visibility -eq "Public") { "Public" } else { "Private" }

    # Add the team's information to the array
    $emptyTeamsData += [pscustomobject]@{
    TeamName    = $team.DisplayName
    Description = $team.Description
    TeamType    = $teamType
    }
    }
    }

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

How the Script Works

This script efficiently retrieves and sorts Microsoft Teams based on the number of owners each team has. Below is a step-by-step explanation of how the script operates:

  1. Connecting to Microsoft Graph: The script begins by establishing a connection to Microsoft Graph using the Connect-MgGraph cmdlet. The required scopes (Team.ReadBasic.All and Group.Read.All) are necessary for reading basic team information and group member data.
  2. Fetching Teams: The script uses the Get-MgGroup cmdlet to retrieve all groups with the Team resource provisioning option. This ensures that only Microsoft Teams are included in the query, and the script retrieves essential properties such as the team's ID, DisplayName, Description, and Visibility.
  3. Checking for Empty Teams: The script iterates through each team and uses the Get-MgGroupMember cmdlet to count the number of members in each team. If a team has no members ($memberCount -eq 0), the script considers it empty and adds its details to an array.
  4. Determining Team Type: The script checks the Visibility property to determine whether a team is Public or Private. Public teams are accessible to anyone in the organization, while Private teams are restricted.
  5. Storing and Displaying Data: For each empty team, the script stores the Team Name, Description, and Team Type in a PowerShell custom object. Finally, the script outputs this information in a neatly formatted table using the Format-Table cmdlet.

Further Enhancements

Here are some possible enhancements you can add to the script:

  • Exporting Data to CSV: You can modify the script to export the list of empty teams to a CSV file for further analysis or documentation purposes. Replace the Format-Table cmdlet with the following:
    $emptyTeamsData | Export-Csv -Path "EmptyTeams.csv" -NoTypeInformation
  • Including Additional Properties: If you require more details about each team, you can expand the Get-MgGroup cmdlet to include additional properties such as MailNickname, CreatedDateTime, or Classification.
  • Automating Team Cleanup: You can further extend the script to automate the archiving or deletion of empty teams based on organizational policies. For example, teams that have been empty for a certain period could be archived automatically.
  • Filtering by Date: You can enhance the script to filter teams based on their creation date, identifying only those that are older than a specific date and empty.

Possible Errors & Solutions

Error: Insufficient Permissions

Cause: If the script fails to connect to Microsoft Graph or retrieve team data, it could be due to insufficient permissions.

Solution: Ensure that your account has the required permissions (Team.ReadBasic.All and Group.Read.All). You might need to re-authenticate or request additional permissions from your administrator.

Error: No Teams Found

Cause: If the script does not return any teams, it could be due to a filtering issue or the absence of teams in your tenant.

Solution: Verify the filtering criteria in the Get-MgGroup cmdlet. Temporarily remove the filter to see if any groups are returned without filtering.

Error: Member Count Incorrect

Cause: The Get-MgGroupMember cmdlet may sometimes not return all members due to API throttling or other issues.

Solution: Implement retry logic or pagination to ensure all members are retrieved. Additionally, check for any API rate limits that might be in place.


Conclusion

This Graph PowerShell script provides a practical solution for identifying empty Microsoft Teams within your organization. By outputting essential details such as Team Name, Description, and Team Type, the script enables administrators to efficiently manage and clean up their Teams environment. With the potential for further enhancements, such as exporting data or automating team cleanup, this script can be tailored to meet specific organizational needs.

By leveraging the power of Microsoft Graph and PowerShell, you can streamline your team's management processes and ensure that your collaboration environment remains organized and efficient.

Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

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