Microsoft Teams Channel Statistics Script Using PowerShell

Managing Microsoft Teams can be a complex task, especially when you need detailed information about each team’s structure, including the number of channels and ownership details. Microsoft Graph PowerShell provides a powerful way to automate and streamline these tasks. In this article, we'll walk through a PowerShell script that retrieves key information about your Teams, such as the team name, description, total number of channels, number of private and standard channels, and the number of owners. This script will help you keep track of your Teams' structure and ensure everything is in order.


The Script

# Ensure you are connected to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All" "Team.ReadBasic.All" "Channel.ReadBasic.All"

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

# Initialize an array to store results
$results = @()

foreach ($team in $teams) {
    $teamId = $team.Id

    # Get all channels in the team
    $channels = Get-MgTeamChannel -TeamId $teamId -Property Id, DisplayName, MembershipType

    # Count channels based on their type
    $totalChannels = $channels.Count
    $privateChannels = $channels | Where-Object { $_.MembershipType -eq 'private' } | Measure-Object | Select-Object -ExpandProperty Count
    $standardChannels = $totalChannels - $privateChannels

    # Get the count of owners
    $owners = Get-MgGroupOwner -GroupId $teamId
    $ownersCount = $owners.Count

    # Create an object with the required information
    $result = [PSCustomObject]@{
        TeamName             = $team.DisplayName
        Description          = $team.Description
        TotalChannels        = $totalChannels
        PrivateChannels      = $privateChannels
        StandardChannels     = $standardChannels
        OwnersCount          = $ownersCount
    }

    # Add the result to the array
    $results += $result
}

# Output the results
$results | Format-Table -AutoSize


How the Script Works

This script efficiently retrieves and sorts Microsoft Teams based on the number of channels and ownership details. 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. This connection is necessary to access the required data, and the script requests permissions to read group, team, and channel data.
  2. Retrieving Teams: The Get-MgGroup cmdlet filters out all groups that are Microsoft Teams by checking the resourceProvisioningOptions property. This ensures that only Teams are retrieved.
  3. Getting Channels: For each team, the script retrieves all channels using Get-MgTeamChannel. It then counts the total channels, the number of private channels, and calculates the number of standard channels by subtracting the private channels from the total.
  4. Counting Owners: The script uses Get-MgGroupOwner to get the number of owners for each team. This is essential for understanding the management structure of each team.
  5. Outputting Results: The script compiles all the gathered information into a custom object and outputs it in a neatly formatted table, showing the team name, description, total number of channels, number of private and standard channels, and the owners count.

Further Enhancements

This script can be further enhanced to provide even more detailed information:

  • Channel Details: You could modify the script to include the names of each channel, whether they are active or archived, and even the creation dates of channels.
  • Members Count: Adding a count of members per team could provide more insight into the size and engagement level of each team.
  • Guest Users: Including a check for guest users within teams could help monitor external access and ensure proper security measures are in place.
  • Export to CSV: The results can be exported to a CSV file for further analysis or record-keeping by adding the Export-Csv cmdlet at the end of the script.

Possible Errors & Solutions

Authentication Issues

Cause: If you encounter an authentication error, ensure you have the necessary permissions and that your session hasn't expired.

Solution: Run Connect-MgGraph again to reauthenticate and verify that you have the required scopes (Group.Read.All, Team.ReadBasic.All, Channel.ReadBasic.All).

Insufficient Permissions

Cause: If the script fails to retrieve data, it might be due to insufficient permissions.

Solution: Make sure your account has the appropriate roles, such as Global Admin or Teams Admin, or adjust the scope of permissions requested during Connect-MgGraph.

No Data Returned

Cause: In some cases, Get-MgTeamChannel or Get-MgGroupOwner may return no data if the team has no channels or owners.

Solution: Ensure that the team exists and has the appropriate configuration, or handle the case where no channels or owners are present in your script.


Conclusion

This PowerShell script provides a comprehensive overview of your Microsoft Teams environment, allowing you to quickly assess the structure and management of each team. By leveraging Microsoft Graph PowerShell, you can automate tedious administrative tasks, ensure consistency across your Teams, and maintain a well-organized environment. Feel free to enhance the script further to meet your specific needs, and always be prepared to handle potential errors gracefully. Happy scripting!


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