Get All Teams and Channels Using Graph PowerShell

Managing Microsoft Teams effectively can be challenging, especially when your organization has many Teams and channels. To streamline this process, administrators can leverage Graph PowerShell to get a comprehensive overview of all Teams and their channels. This article introduces a simple yet powerful script that retrieves all Teams and their respective channels, helping administrators keep their Teams environment organized.

The Script

Below is the Graph PowerShell script that retrieves a list of all Teams in your organization along with their associated channels:

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

# Retrieve all Teams in the organization
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -All

# Loop through each team to get its channels
foreach ($team in $teams) {
    Write-Output "Team Name: $($team.DisplayName)"
    Write-Output "Team ID: $($team.Id)"

    # Retrieve channels for the current team
    $channels = Get-MgTeamChannel -TeamId $team.Id

    if ($channels) {
        Write-Output "Channels:"
        foreach ($channel in $channels) {
            Write-Output " - $($channel.DisplayName) (Channel ID: $($channel.Id))"
        }
    } else {
        Write-Output " - No channels found."
    }
    Write-Output "-------------------------------------------"
}

# Disconnect from Microsoft Graph
Disconnect-MgGraph

How the Script Works

  • Connect-MgGraph: The script starts by connecting to Microsoft Graph using the necessary permissions to read Teams, groups, and channel data.
  • Retrieve All Teams: It uses the Get-MgGroup cmdlet to fetch all groups that have been provisioned as Teams by applying a filter on the resourceProvisioningOptions property.
  • Loop Through Each Team: For each Team retrieved, the script displays its name and ID, then proceeds to fetch all the channels associated with that Team using the Get-MgTeamChannel cmdlet.
  • Display the Channels: If channels are found, they are listed under the corresponding Team. If no channels exist, a message indicating the absence of channels is displayed.
  • Disconnect: Finally, the script disconnects from Microsoft Graph to maintain a secure session.

Frequently Asked Questions

  • Can I retrieve both Teams and Channels in a single command?
  • No. Teams and Channels are separate objects in Microsoft Graph. You must first query Teams using Get-MgTeam and then query their respective channels with Get-MgTeamChannel

  • How can I filter out the default General channel?
  • When processing the results, you can filter out entries where the displayName is "General". This ensures you only capture custom-created channels.

  • Is it possible to retrieve archived Teams as well?
  • Yes. Archived Teams are still listed when you run Get-MgTeam. To identify them, check the isArchived property for each Team.

  • What’s the best way to handle large tenants with many Teams and Channels?
  • Use the -All parameter or implement $skiptoken paging to handle larger datasets. Exporting results to CSV can also help with further analysis and reporting.


Possible Errors & Solutions

Error Cause Solution
Insufficient privileges to complete the operation. The account you're using doesn't have the required permissions to access Teams or channels data. Ensure that you have the necessary permissions such as Group.Read.All, Team.ReadBasic.All, and Channel.ReadBasic.All. You may need to contact your admin to grant these permissions or use an account with sufficient rights.
Get-MgTeamChannel : Resource not found. The Team ID being used does not exist or cannot be accessed with your current permissions. Verify that the Team ID is correct and that you have the appropriate permissions to view the channels within that Team.
Authorization_RequestDenied The authentication token used by the script does not have the correct permissions. Reconnect to Microsoft Graph using the Connect-MgGraph cmdlet and ensure you request the necessary scopes during the connection process.

Further Enhancements

  • Export to CSV: Modify the script to export the results to a CSV file for better documentation or reporting.
  • Filtering Options: Add filtering options to list only specific Teams or channels based on certain criteria (e.g., created date or owner).
  • Channel Details: Include additional channel details like the description, last activity date, and membership count.
  • Automated Reports: Set up this script to run on a schedule and generate periodic reports to monitor the Teams environment.

✅ Teams vs. Channels Data Separation

When retrieving both Teams and their channels, remember that Teams and Channels are separate objects in Microsoft Graph.

You’ll need to first query Teams (Get-MgTeam) and then loop through each Team to fetch its Channels (Get-MgTeamChannel). This separation helps ensure accuracy when building detailed reports.
✅ System Channels Are Retrieved Too

By default, Get-MgTeamChannel also returns system-generated channels like General.

If you’re only interested in custom-created channels, apply filters (e.g., exclude channels with displayName “General”) when processing results to keep your output relevant.

Conclusion

This simple Graph PowerShell script provides administrators with a quick way to audit and manage their Teams and channels. By leveraging Microsoft Graph, you can easily adapt and extend this script to meet your organization's specific needs. Utilizing scripts like this helps maintain a well-structured Teams environment, ensuring that users can collaborate efficiently.

Suggested Reading

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