Discover all the private channels on Microsoft Teams

Creating private channels is possible by default on your organisation and probably all organisation will have these types of the channel on Microsoft Teams. But from a governance perspective that can leave the user to create those without any criteria. So on this post, we will share a PowerShell script that will allow getting all the private channels on your organisation.

This information can be extracted from multiple ways so we gonna use the PnP PowerShell to get us a token and from there, make a request to Microsoft Graph that will allow us to receive this information that we’re looking for.

Before you run this on your organisation make sure, you’ve permissions to accept the application into your Tenant.

We will make a request with the filter on Microsoft Graph, that allows us just to get the private channels and not all the channels.

Connect-PnPOnline -Scopes ("Group.Read.All", "User.ReadBasic.All")
$AccessToken = Get-PnPGraphAccessToken

$GetAllTeams = Invoke-RestMethod -Headers @{Authorization = "Bearer $AccessToken" } -Uri "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/any(c:c+eq+`'Team`')"

$ChannelsList = @()

foreach ($Team in $GetAllTeams.value) {

    $ResponseTeamsChannels = Invoke-RestMethod -Headers @{Authorization = "Bearer $AccessToken" } -Uri ("https://graph.microsoft.com/beta/teams/{0}/channels?`$filter=membershipType eq `'private`'" -f $Team.id)
    $ResponseTeamsChannels.value.forEach( {
    if ($_.membershipType -eq "private") {
        $ChannelObject = "" | Select-Object "TeamName", "TeamId", "ChannelName",  "ChannelId", "Description"
        $ChannelObject.TeamName = $Team.displayName
        $ChannelObject.TeamId = $Team.id
        $ChannelObject.ChannelName = $_.displayName
        $ChannelObject.ChannelId = $_.id
        $ChannelObject.Description = $_.description
        $ChannelsList += $ChannelObject
    } 
    })    
}

$ChannelsList | Export-Csv -Path './PrivateChannels.csv' -NoTypeInformation

Conclusion

With this simple PowerShell we have now a report of the private channels and we can use this information to implement some governance, understand if those groups are being used.

3 Comments

  1. Jan said:

    Connect-PnPOnline -Scopes (“Group.Read.All”, “User.ReadBasic.All”) generates an error….

    July 18, 2022
    Reply
  2. Murali said:

    Where MS graph needs to be installed and how it will work

    November 14, 2022
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *