List All Teams and Channels

In this article I am going to show you how to list all Microsoft Teams and Channels in an Microsoft 365 tenant. I will show two approaches to export this information as a csv file: PowerShell vs Power Automate.

Inspiration

This question from PatrickJMcLean:

I am building a flow to collect all the Teams in our 365 Tenant, and any channels within those Teams. I have no problem exporting the Teams to a table in Excel, but the problem comes when I try to export the channels.

Power Users Community thread: Extracting All Teams and Their Channels.

PowerShell

First of all you could retrieve this information with Microsoft Teams PowerShell. This module contains the Get-Team & Get-TeamChannel cmdlets. Below is a PowerShell script based on a sample from gcjuw84 on Techcommunity forums. Feel free to use the PowerShell script if your more a cmd line type of person.

Graph API

However, in this case I also tried to solve it via Power Automate, just for the challenge ;). Within this Power Automate flow the List all teams in Microsoft Teams for an organization and the List Channels methods of the Graph API are used.

The flow steps

Before you start:
1. This sample assumes the account used has Global admin or Microsoft Teams admin permissions
2. Be aware the HTTP action is part of a premium connector.
3. The HTTP actions use Active Directory OAuth authentication.

You need to register an App in Azure Active Directory with the appropriate scope permissions. Like mentioned in a couple of my other blogposts, Elaiza Benitez has written a nice blogpost about this, How to authenticate as an application with Microsoft Graph API with flow.

listteamsandchannels_flow

1. Add a Recurrence trigger action.

This flow will run once a week on Monday at 07:00.

recurrence_teams

a. Select an Interval, in this setup 1
b. Select the Frequency, in this setup Week
c. Select the Days, in this setup Monday
d. Select the Hours, in this setup 07

2. Add a Initialize variable action.

This array is used to temporary store all the details from the GET actions.

initializevariable_teams

a. Provision a Name for the variable, in my setup AllTeamsandChannelsArray
b. Select the Array Type

3. Add a HTTP action.
This action is to retrieve all the Teams via the Graph API.

getallteams_graphapi

a. Select the GET method
b. Use the URI from the codesnippet below
c. Use the Headers from the codesnippet below
d. Select Active Directory OAuth for the Authentication, provide the details of your Azure AD App.

4. Add a Apply to Each action.
This action is to loop through the collection of teams and retrieve all the channels per team.

applytoeach_value

a. Use the body/value for the Select an output from previous steps.

5. Add a second HTTP action (within the Apply to Each action).
In this HTTP action we use TeamId to retrieve the channels per team

getallchannels_graphapi

a. Select the GET method
b. Use the URI from the codesnippet below
c. Use the Headers from the codesnippet below
d. Select Active Directory OAuth for the Authentication, provide the details of your Azure AD App.

6. Add a Select action (within the Apply to Each action).
In this action we will collect both details of the Team and of the Channel. Every Channel will have it’s own row in the CSV file.

select_teams

a. Use the body/value from the previous HTTP action in the From field.
b. Switch the Map field to text mode
c. Use the codesnippet below in the Map field

7. Add a Compose action (within the Apply to Each action).
A union function is used to add all array items into one single Array from our array variable.

compose_union

a. Use the expression from the codesnippet below for the inputs

8. Add a Set Variable action (within the Apply to Each action).
In this action we will store the output from the compose into the variable.

setvariable_teams

a. Select the AllTeamsandChannelsArray variable
b. Use the Outputs of the Compose action for the Value field.

9. Add a Create CSV table action.
From the Array variable a CSV table is generated.

createcsvtable_teams

a. Use the AllTeamsandChannelsArray for the From field.

10. Add a Create file action.
The CSV table content will be used to create a file. In this case on SharePoint, but OneDrive for Business would be another option.

createfile_teams

a. Select the SharePoint Site where you want to store the file.
b. Select the Folder Path
c. Provide a file name, I used the following expression: d. Use the Output of the Create CSV table in the File Content field.

Testing

That is it for the setup of this example.

The output when you run the flow or the PowerShell script should look something like below. createfile_teams_output

Happy testing!

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.