Microsoft Teams is widely used for team collaboration, and being able to fetch detailed information about team channels, especially the owners, can help administrators manage and monitor these channels effectively. This article provides a PowerShell script using Microsoft Graph to list all Teams channels and their respective owners.
Here's a PowerShell script that connects to Microsoft Graph, retrieves all channels across all Teams in the tenant, and lists the owners of each channel:
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All", "Channel.ReadBasic.All", "ChannelMember.Read.All", "User.Read.All"
# Get all teams in the tenant
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -Property Id DisplayName -All
# Initialize an array to store the details
$channelsDetails = @()
foreach ($team in $teams) {
# Get all channels in the team using the Graph API
$channels = Invoke-MgGraphRequest -Method Get -Uri "https://graph.microsoft.com/v1.0/teams/$($team.Id)/channels"
foreach ($channel in $channels.value) {
# Get the members of the channel
$members = Invoke-MgGraphRequest -Method Get -Uri "https://graph.microsoft.com/v1.0/teams/$($team.Id)/channels/$($channel.id)/members"
# Filter out owners
$owners = $members.value | Where-Object { $_.roles -contains "owner" }
# Add the channel details to the array
$channelsDetails += [PSCustomObject]@{
TeamName = $team.DisplayName
ChannelName = $channel.displayName
Owners = ($owners | ForEach-Object { $_.displayName }) -join " "
}
}
}
# Display the details in a tabular format
$channelsDetails | Format-Table -AutoSize
# Export the details to a CSV file (optional)
$channelsDetails | Export-Csv -Path "TeamsChannelsOwnersDetails.csv" -NoTypeInformation
Write-Output "Details exported to TeamsChannelsOwnersDetails.csv"
Script Output
The script starts by connecting to Microsoft Graph using Connect-MgGraph
with the required scopes (Group.Read.All
, Channel.ReadBasic.All
, ChannelMember.Read.All
,User.Read.All
).
#Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All", "Channel.ReadBasic.All", "ChannelMember.Read.All", "User.Read.All"
It fetches all Teams in the tenant by filtering groups that have the resourceProvisioningOptions
property set to 'Team'.
# Get all teams in the tenant
$teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')" -Property Id DisplayName -All
For each Team, the script retrieves all channels using the Graph API.
$channels = Invoke-MgGraphRequest -Method Get -Uri "https://graph.microsoft.com/v1.0/teams/$($team.Id)/channels"
It then retrieves the members of each channel and filters out the owners.
# Get the members of the channel
$members = Invoke-MgGraphRequest -Method Get -Uri "https://graph.microsoft.com/v1.0/teams/$($team.Id)/channels/$($channel.id)/members"
# Filter out owners
$owners = $members.value | Where-Object { $_.roles -contains "owner" }
The details of each channel, including the team name, channel name, and owners, are stored in an array and displayed in a tabular format.
# Display the details in a tabular format
$channelsDetails | Format-Table -AutoSize
The script includes an option to export the details to a CSV file named TeamsChannelsOwnersDetails.csv
.
# Export the details to a CSV file (optional)
$channelsDetails | Export-Csv -Path "TeamsChannelsOwnersDetails.csv" -NoTypeInformation
Error: Failure to connect to Microsoft Graph.
Solution: Ensure that the necessary permissions are granted and the user has the appropriate roles.
Error: Too many requests in a short period.
Solution: Implement retry logic with exponential backoff to handle rate limits.
Error: Missing or partial data.
Solution: Ensure the script handles pagination when retrieving large sets of data from the API.
Fetching the owners of Microsoft Teams channels is a valuable task for administrators. The provided PowerShell script makes it easy to gather and review this information. By enhancing and integrating the script into regular administrative tasks, organizations can maintain better control over their collaboration environment. Whether for audit purposes, security monitoring, or administrative reporting, this script provides a solid foundation for managing Teams channel ownership.
© m365corner.com. All Rights Reserved. Design by HTML Codex