Extract All Teams and Channels Details using Graph API (PowerShell) in your Organization

Tanmoy Naiya
3 min readAug 2, 2024

--

Extract All Teams and Channels Details using Graph API (PowerShell) in your Organization

Did you ever faced the challenge as a Microsoft365 admin when someone came to you asking for a Teams channel membership but dont know the Teams name where the channel belongs!! Yes, it is really frustrating to locate the channel manually from the Teams Admin center.

Today I will discuss, how we can extract a full list of Teams and Channels details using Graph API and write the full PowerShell script so that it can be handy when we face this kind of requirements. Hoping that, this will ease some of Microsoft 365 admins life at some point.

Basic Requirements

To achive this functionality, we need very basic things as below.
1. Register an App in Azure.
2. Get the permissions mentioned in Graph API documentation by Microsoft. (We need two kind of permission, one for getting all the groups details which has Teams and another for listing all channels inside a Teams).
3. Get the admin consent for all the permissions from a global admin(if you are not a Global admin).

Get the Access Token

Unfortunately, this doesn’t supported for delegated permission. So we have to get the Access Token for Application Permission. To generate the Access Token you can follow this article.

If you are looking for generating Access Token for Delegated permission then you can follow this article.

List all the Groups which has Teams

To get all the Teams details in your organization, you need to get all the groups in your organization and then filter the groups which has Teams attached with it. We can do that by a very simple PowerShell script like below.

# get the access token, to get the token Follow the article I mentioned above

$token = get_token

# Get all the groups and filter only Teams Groups

$teamsURL = "https://graph.microsoft.com/v1.0//groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"

do{
$teams = Invoke-RestMethod -Method GET -Uri $teamsURL -Headers @{Authorization = "Bearer $token"} -ContentType 'application/json'
$teamsURL = $teams.'@odata.nextLink'
}
until(!$teamsURL)

Common mistake

A common mistake might happen here while running this query in PowerShell if you did not copy exactly from above script and only followed Microsoft documentation. Did you notice the “ ` ” sign between ? and $ like below.
if you ignore this, PowerShell will think that $filter is a variable and it will corrupt your URL, so you will not get your desired filtered result.

be careful

Loop through all Teams and get all channels

Now that we have got all the Teams details in our organization, we can loop through all the Teams and get all the channels in it like below.

Full PowerShell Script


# Get the date and Script location (Optional)
$date = Get-Date -Format 'yyyyMMdd'
$path = $PSScriptRoot

# get the access token, to get the token Follow the article I mentioned above
$token = get_token

# Get all the groups and filter only Teams Groups

$teamsURL = "https://graph.microsoft.com/v1.0//groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"

do{
$teams =Invoke-RestMethod -Method GET -Uri $teamsURL -Headers @{Authorization = "Bearer $token"} -ContentType 'application/json'
$teamsURL = $teams.'@odata.nextLink'

foreach($team in $teams.value){
# get the channels
$channelURL = "https://graph.microsoft.com/v1.0/teams/$($team.id)/channels"
$channels =Invoke-RestMethod -Method GET -Uri $channelURL -Headers @{Authorization = "Bearer $token"} -ContentType 'application/json'
foreach($channel in $channels.value){
$obj = [PSCustomObject]@{
TeamsID = $team.id
TeamsName = $team.displayName
TeamsVisibility = $team.visibility
TeamsDescription = $team.description
ChannelID = $channel.id
ChannelName = $channel.displayName
ChannelType = $channel.membershipType
ChannelDescription = $channel.description
isArchive = $channel.isArchived
}
$obj | fl
$obj | Export-Csv "$path\All_Teams_Channels_Details_$date.csv" -Append -NoTypeInformation -Encoding UTF8
}
}

}
until(!$teamsURL)

You should get details printed on your screen like below.

Teams and Channels Details
Teams and Channels details

Thanks for reading this article till here..

Comment if you have any queries!!

--

--

Tanmoy Naiya

Tanmoy is an M365 and PowerShell expert having more than 9 years of industrial experience. He helped organizations extensively with M365 solutions & automations