Export Private Channels and Members from All Teams using PowerShell

Private channels in Microsoft Teams support a restricted space for collaboration in a team that’s only available to a subset of team members. Only the users on the team who are owners or members of the private channel can access the channel. In this post, we will explore how to discover the private channels in all the teams and export members of the private channels to a CSV file using PowerShell script.

Before you start, install the latest Microsoft Teams PowerShell module, and run the following command to connect to the Teams module.

Connect-MicrosoftTeams

Summary

Find and Export All Private Channels in Microsoft Teams

As of now, there is no way to identify only the Teams that have private channels. So, we need to get all the teams using the Get-Team cmdlet and iterate the teams one by one to retrieve the private channels using the Get-TeamChannel cmdlet.

The below script discovers the private channels in all the teams and exports the result to a CSV file.

$Result = @() #Result array

#Get all teams
$AllTeams= Get-Team
$TotalTeams = $AllTeams.Count
$i = 0

#Iterate teams one by one and gets private channels 
ForEach ($Team in $AllTeams)
{
$i++
Write-Progress -Activity "Discover private channels in $($Team.Displayname)" -Status "$i out of $TotalTeams teams completed"

Try
{
#Get private channels
$PrivateChannels = Get-TeamChannel -GroupId $Team.GroupId -MembershipType Private
 
#Iterate channels one by one and adds to the result array
ForEach ($Channel in $PrivateChannels)
{
#Add channel info to the result array
$Result += New-Object PSObject -property $([ordered]@{
TeamName = $Team.DisplayName
ChannelName = $Channel.DisplayName
Description = $Channel.Description
GroupId = $Team.GroupId
ChannelId = $Channel.Id
})
 
}
}
Catch 
{
Write-Host "Error occurred for $($Team.Displayname)" -f Yellow
Write-Host $_ -f Red
}
}
 
#Export the result to CSV file
$Result | Export-CSV "C:\Temp\AllPrivateChannels.CSV" -NoTypeInformation -Encoding UTF8

Export All Private Channels and their Members

We can use the Get-TeamChannelUser cmdlet to retrieve members and owners of a private channel. The below script gets all the private channels and their members and exports the result to a CSV file.

$Result = @() #Result array

#Get all teams
$AllTeams= Get-Team
$TotalTeams = $AllTeams.Count
$i = 0

#Iterate teams one by one and gets private channels 
ForEach ($Team in $AllTeams)
{
$i++
Write-Progress -Activity "Fetching private channels from $($Team.Displayname)" -Status "$i out of $TotalTeams teams completed" -Id 1

Try
{
#Get private channels
$PrivateChannels = Get-TeamChannel -GroupId $Team.GroupId -MembershipType Private
$TotalChannels = $PrivateChannels.Count
$ch=0

#Iterate channels one by one and retrieves membership
ForEach ($Channel in $PrivateChannels)
{
$ch++
Write-Progress -Activity "Fetching members from $($Channel.Displayname)" -Status "$ch out of $TotalChannels Channels completed" -ParentId 1

Try
{
#Get channel members and owner
$ChannelUsers = Get-TeamChannelUser -GroupId $Team.GroupId -DisplayName $Channel.DisplayName
 
#Iterate users one by one and add to the result array
ForEach ($ChannelUser in $ChannelUsers)
{
#Add user info to the result array
$Result += New-Object PSObject -property $([ordered]@{
TeamName = $Team.DisplayName
ChannelName = $Channel.DisplayName
UserName = $ChannelUser.Name
UserPrincipalName = $ChannelUser.User
Role = $ChannelUser.Role
GroupId = $Team.GroupId
ChannelId = $Channel.Id
UserId = $ChannelUser.UserId
})
}
}
Catch 
{
Write-Host "Error occurred for $($Team.Displayname) - $($Channel.Displayname)" -f Yellow
Write-Host $_ -f Red
}
}

}
Catch 
{
Write-Host "Error occurred for $($Team.Displayname)" -f Yellow
Write-Host $_ -f Red
}
}
 
#Export the result to CSV file
$Result | Export-CSV "C:\Temp\AllPrivateChannelMembers.CSV" -NoTypeInformation -Encoding UTF8
Advertisement

5 thoughts on “Export Private Channels and Members from All Teams using PowerShell”

  1. `Get-TeamChannel -GroupId $Team.GroupId -MembershipType Private`

    -MembershipType is not a parameter of Get-TeamChannel, probably it was deprecated.

    Reply
    • No, this parameter is available in the latest Teams PowerShell module. You can uninstall the old module and install the latest one.

      # Uninstall all versions of the module
      Uninstall-Module MicrosoftTeams -Allversions

      # Install the latest Teams module
      Install-Module -Name MicrosoftTeams -Force -AllowClobber

      Reply
  2. Get-Team is much slower when you have lots of objects

    Gathered 10000 Microsoft Teams in 9 seconds with Get-MgGroup. Get-Team takes at least 15 minutes.

    Reply
  3. Very good script. I have used it many times. Unfortunately, for some time the script stopped working. While the script is running, I randomly receive messages about lack of authorization. I tried on several computers and on different accounts with Global Admin privileges. Messages when downloading private channel members:
    “Error occurred for .
    Error occurred while executing
    Code: Unauthorized
    Message: Identity required
    InnerError:
    RequestId: e5e08b39-1bd6-4b9d-a76e-yyyyyyyy
    DateTimeStamp: 2023-09-11T09:46:12
    HttpStatusCode: Unauthorized”

    Every time I run the script the errors appear with other teams.

    Reply

Leave a Comment