List Private Channel Owners Across Microsoft Teams

Managing private channels in Microsoft Teams is a crucial task for IT administrators, especially when ensuring that only authorized users have ownership rights. Private channels are a popular feature within Microsoft Teams, allowing a subset of team members to have focused conversations and file sharing. However, keeping track of the owners of these private channels can be challenging.

In this article, we’ll walk you through a Graph PowerShell script that lists all private channel owners across Teams within your tenant. This script outputs the results in a tabular format, including the Team Name, Channel Name, Owner Name, and Owner's Email.


The Script

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Team.ReadBasic.All" "ChannelMember.Read.All" "User.Read.All"

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

$results = @()

# Iterate through each team
foreach ($team in $teams) {
    if ($team.Id) {
        $channels = Get-MgTeamChannel -TeamId $team.Id
        
        foreach ($channel in $channels) {
            if ($channel.MembershipType -eq "private") {
                $members = Get-MgTeamChannelMember -TeamId $team.Id -ChannelId $channel.Id
                
                # Filter members to only include owners
                $owners = $members | Where-Object { $_.Roles -contains "owner" }
                
                foreach ($owner in $owners) {
                    $results += [PSCustomObject]@{
                        "Team Name"   = $team.DisplayName
                        "Channel Name"= $channel.DisplayName
                        "Owner Name"  = $owner.DisplayName
                        "Owner's Mail"= $owner.Email
                    }
                }
            }
        }
    }
}

# Display results in a table
$results | Format-Table -AutoSize

# Export results to a CSV file if needed
# $results | Export-Csv -Path "PrivateChannelOwners.csv" -NoTypeInformation

How the Script Works

This script effectively lists all private channel owners in Microsoft Teams. Here’s how it operates:

  1. Connecting to Microsoft Graph: The script starts by connecting to Microsoft Graph using the Connect-MgGraph cmdlet. The necessary permissions (Team.ReadBasic.All, ChannelMember.Read.All, and User.Read.All) are specified to allow reading of teams, channel members, and user details.
  2. Retrieving All Teams: The script retrieves all the teams within the tenant using the Get-MgGroup cmdlet. The filter ensures that only teams are retrieved.
  3. Iterating Through Each Team: For each team retrieved, the script checks if the team has a valid Id. It then retrieves all the channels within the team using the Get-MgTeamChannel cmdlet.
  4. Identifying Private Channels: The script identifies private channels by checking the MembershipType property of each channel.
  5. Retrieving and Filtering Channel Owners: The script retrieves all members of each private channel using the Get-MgTeamChannelMember cmdlet. It then filters these members to include only those with the role of "owner".
  6. Collecting the Results: For each owner identified, the script stores the Team Name, Channel Name, Owner Name, and Owner’s Email in a custom object. These objects are collected into an array for output.
  7. Outputting the Results: Finally, the script displays the results in a table format using Format-Table and provides an option to export the results to a CSV file.

Further Enhancements

This script is a solid foundation for managing private channel ownership. However, there are several enhancements you could consider:

  • Email Notifications: You could extend the script to send an email notification to the administrators or the team owners with the list of private channel owners.
  • Scheduling: Automate this script to run on a regular basis (e.g., weekly) using Azure Automation or a scheduled task.
  • Logging: Implement logging to record when and how the script was run and store the output in a central location for future reference.
  • User Interaction: Add prompts or inputs to allow the script user to filter results by specific teams or channels, or to choose between displaying results on-screen or saving them to a file.

Possible Errors & Solutions

Cannot bind argument to parameter 'TeamId' because it is an empty string.

Cause: This error occurs if a team does not have a valid Id or if the team retrieval step failed.

Solution: Ensure that the $team.Id is not empty by adding a check before using it in subsequent commands.

A parameter cannot be found that matches parameter name 'Role'.

Cause: The Get-MgTeamChannelMember cmdlet does not support a -Role parameter.

Solution: Remove the -Role parameter and manually filter the members based on their roles using Where-Object.

Insufficient permissions to perform the requested operation.

Cause: The account running the script does not have the necessary permissions.

Solution: Ensure that the account has been granted the required permissions when connecting to Microsoft Graph.


Conclusion

This PowerShell script provides a practical solution for IT administrators to manage and audit private channel ownership across Microsoft Teams within their tenant. By understanding who has ownership rights to private channels, you can maintain better control and security over sensitive information shared within your organization. With the provided enhancements, you can tailor the script to fit your specific needs, making it an even more powerful tool in your administration toolkit.

Feel free to adapt and expand the script based on your organization's requirements and don't hesitate to share your improvements and use cases with the community. 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

``