Graph PowerShell Script for Monitoring Team Data

Microsoft Teams has become an essential collaboration tool for organizations, allowing teams to communicate and collaborate efficiently. As an IT administrator, keeping track of the various teams within your organization is crucial. In this article, we'll explore a Graph PowerShell script that queries Microsoft Teams and outputs essential data such as Team Name, Description, Team Type, and Member Count in a tabular format. This script will help you monitor and manage your Teams environment more effectively.


The Script

# Connect to Microsoft Graph with the necessary scopes
Connect-MgGraph -Scopes "Team.ReadBasic.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 team data
$teamData = @()

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

    # Determine the team type based on visibility
    $teamType = if ($team.Visibility -eq "Public") { "Public" } else { "Private" }

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

# Output the data in a table format
$teamData | 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 Team.ReadBasic.All scope is required to read basic information about teams in the organization.
  2. Fetching Teams: The Get-MgGroup cmdlet is used to retrieve all groups that have a Team resource provisioning option. This ensures that only Microsoft Teams groups are fetched, along with their ID, DisplayName, Description, and Visibility properties.
  3. Retrieving Member Count: For each team, the script uses the Get-MgGroupMember cmdlet to count the number of members. The -All parameter ensures that all members are retrieved, regardless of how many there are.
  4. Determining Team Type: The script determines whether the team is Public or Private based on the Visibility property. Public teams are accessible to anyone in the organization, while Private teams have restricted access.
  5. Storing and Displaying Data: The script stores the retrieved data in a PowerShell custom object and outputs it 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 easily export the output to a CSV file for further analysis or reporting. Simply replace the Format-Table cmdlet with the following:
    $teamData | Export-Csv -Path "TeamsData.csv" -NoTypeInformation
  • Adding More Properties: If you need more information about each team, you can expand the Get-MgGroup cmdlet to include additional properties such as MailNickname, CreatedDateTime, or Classification.
  • Filtering by Specific Teams: You can modify the script to filter and display only specific teams based on criteria such as DisplayName or Visibility. For example:
    $teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team') and DisplayName eq 'Sales'" -Property Id, DisplayName, Description, Visibility -All

Possible Errors & Solutions

Authentication Failed

Cause: This error occurs if the user does not have sufficient permissions or if authentication fails during the Connect-MgGraph cmdlet.

Solution: Ensure that you have the necessary permissions (Team.ReadBasic.All scope) and that your credentials are correct. You may also need to re-authenticate using Disconnect-MgGraph and then reconnect.

No Teams Found

Cause: If the script returns no teams, it might be due to a filtering issue or that no teams exist in the tenant.

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

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 powerful way to query and manage Microsoft Teams within your organization. By displaying essential data such as Team Name, Description, Team Type, and Member Count, you can gain valuable insights into your Teams environment. With potential enhancements like exporting data or adding more properties, this script can be tailored to meet specific administrative needs.

By leveraging the power of Microsoft Graph and PowerShell, you can automate and streamline your team's management tasks, making your job as an IT administrator more efficient and effective.

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