Export Channels and Members from All Teams using PowerShell

Administrators can use the Microsoft Teams Admin center to view and manage Teams and Channels. In this post, we will explore how to export teams, channels, and members from all the teams to a CSV file using PowerShell.

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

Connect-MicrosoftTeams

Summary

Export All Teams

We can use the Get-Team cmdlet to retrieve all teams in the organization. Use the below command to list all the teams.

#List all the teams 
Get-Team

#List the private teams
Get-Team -Visibility Private

#List the archived teams
Get-Team -Archived $true

Run the following command to export a list of all teams to a CSV file.

Get-Team | Export-CSV "C:\AllTeams.CSV" -NoTypeInformation -Encoding UTF8

Export Channels from All Teams

We can use the Get-TeamChannel cmdlet to get all the channels for a single team. To retrieve channels from all the teams, we need to get all teams and iterate the teams one by one to get channels. The below script retrieves the channels from all the teams and exports the result to a CSV file.

$Result = @()
#Get all teams
$AllTeams= Get-Team
$TotalTeams = $AllTeams.Count
$i = 0
#Iterate teams one by one and get channels 
ForEach ($Team in $AllTeams)
{
$i++
Write-Progress -Activity "Fetching channels from $($Team.Displayname)" -Status "$i out of $TotalTeams completed"
Try
{
#Get channels
$TeamChannels = Get-TeamChannel -GroupId $Team.GroupId

#Iterate channels one by one and add to the result array
ForEach ($Channel in $TeamChannels)
{
#Add channel info to the result array
$Result += New-Object PSObject -property $([ordered]@{
TeamName = $Team.DisplayName
GroupId = $Team.GroupId
TeamVisibility = $Team.Visibility
ChannelName = $Channel.DisplayName
ChannelId = $Channel.ChannelId
})

}
}
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\AllTeamChannels.CSV" -NoTypeInformation -Encoding UTF8

Export Members and Owners from All Teams

We can use the Get-TeamUser cmdlet to retrieve users of a team. To retrieve users for all the teams in your organization, we need to get the teams list and iterate the teams one by one to get users. The below script gets the members and owners from all the teams and exports the result to a CSV file.

$Result = @()
#Get all teams
$AllTeams= Get-Team
$TotalTeams = $AllTeams.Count
$i = 0
#Iterate teams one by one and get channels 
ForEach ($Team in $AllTeams)
{
$i++
Write-Progress -Activity "Fetching users for $($Team.Displayname)" -Status "$i out of $TotalTeams completed"
Try
{
#Get team users
$TeamUsers = Get-TeamUser -GroupId $Team.GroupId

#Iterate users one by one and add to the result array
ForEach ($TeamUser in $TeamUsers)
{
#Add user info to the result array
$Result += New-Object PSObject -property $([ordered]@{
TeamName = $Team.DisplayName
TeamVisibility = $Team.Visibility
UserName = $TeamUser.Name
UserPrincipalName = $TeamUser.User
Role = $TeamUser.Role
GroupId = $Team.GroupId
UserId = $Team.UserId
})
}
}
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\AllTeamMembers.CSV" -NoTypeInformation -Encoding UTF8
Advertisement