Fetch Microsoft Teams Channel Owners Using Graph PowerShell

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.


The Script

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


Script Explanation

Connect to Microsoft Graph:

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"

Retrieve Teams:

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

Retrieve Channels:

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"

Retrieve Members and Filter Owners:

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" }

Store and Display Details:

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

Export to CSV (Optional):

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

Further Enhancements

  • Include Additional Details: The script can be modified to include additional details such as the channel description, member count, and creation date.
  • Filter Specific Teams or Channels: Enhance the script to filter and display information for specific teams or channels based on user input.
  • Schedule Script Execution: Use Task Scheduler or Azure Automation to run the script at regular intervals and send the output via email.
  • Error Handling and Logging: Implement error handling to manage API call failures and log errors for troubleshooting.

Use Cases

  • Audit and Compliance: Regularly audit channel owners to ensure compliance with organizational policies.
  • Security Monitoring: Monitor channel ownership to detect unauthorized changes or additions.
  • Administrative Reporting: Generate reports for team administrators to review channel ownership and make necessary updates.

Possible Errors & Solutions

Authentication Issues:

Error: Failure to connect to Microsoft Graph.

Solution: Ensure that the necessary permissions are granted and the user has the appropriate roles.

API Throttling:

Error: Too many requests in a short period.

Solution: Implement retry logic with exponential backoff to handle rate limits.

Incomplete Data Retrieval:

Error: Missing or partial data.

Solution: Ensure the script handles pagination when retrieving large sets of data from the API.


Conclusion

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.


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