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