Microsoft Teams : Fetching all Teams using Microsoft Graph API through PowerShell

What ever you decide to do, make sure it makes you happy.
Hello Everyone,
Hope you all are doing well.
In this article we are going to discuss how to register an app in Azure Directory, how to access Teams using Microsoft Graph and generate csv file through PowerShell.
So without getting late, let’s get started.
Background
- Microsoft Graph is the gateway to data and intelligence in Microsoft 365.
- It provides a unified programmability model that you can use to access the tremendous amount of data in Microsoft 365, Windows, and Enterprise Mobility + Security.
- Use the wealth of data in Microsoft Graph to build apps for organizations and consumers that interact with millions of users.
Microsoft Graph exposes REST APIs and client libraries to access data on the following Microsoft cloud services:
- Microsoft 365 core services: Bookings, Calendar, Delve, Excel, Microsoft 365 compliance eDiscovery, Microsoft Search, OneDrive, OneNote, Outlook/Exchange, People (Outlook contacts), Planner, SharePoint, Teams, To Do, Viva Insights
- Enterprise Mobility + Security services: Advanced Threat Analytics, Advanced Threat Protection, Azure Active Directory, Identity Manager, and Intune
- Windows services: activities, devices, notifications, Universal Print
- Dynamics 365 Business Central services
To find out more, see our other articles https://knowledge-junction.in/?s=microsoft+graph.

Graph API can be used to automate the Microsoft Teams lifecycle such as creating teams, channels, adding members etc.
Refer to this link to see the list of Graph API’s available for Microsoft Teams.
See how to list all teams here.
Details
In this article, we will discuss how to perform the following tasks,
- Register an Application in Azure — Register an app in Azure AD and add the required permissions to access the Graph API.
- Create and execute the PowerShell script
Register an application in Azure
Register an application in Azure AD to access the Teams Graph API.
- Navigate to Azure portal.
- Search for App Registrations under Manage. Click App Registrations as show below.
- Click New Registration.

- Enter the Name and click Register.

- App is registered successfully. In the left navigation, click API Permissions under Manage.

- Click Add a permission.

- Select Microsoft Graph API as shown below.

- Click Application Permissions.

- Select Directory.Read.All permissions and click Add permissions.

- Click Grant admin consent.


- In the left navigation, click Overview. Copy the Application (client) ID value. This value will be used in PowerShell for authentication.

- In the left navigation, click Certificates & secrets under Manage. Click New client secret.

- Enter the description and click Add.

- Copy the secret value which will be used in PowerShell for authentication.

Create and execute the PowerShell script
Prerequisites
- Install PowerShell ISE / PowerShell / Visual Studio Code
- Install Module Microsoft Graph
- Install the Module Microsoft Graph Teams
Complete Script
- Following is the PowerShell script getting all Tenant Teams with detailed comments.
<#
====================================================================================================
Name: Fetching all Tenant groups having Team using Microsoft Graph API through PowerShell
Description: This script searches for all tenant groups having Team in an Organization
Version: 1.0
====================================================================================================
#>
# Install the Module Microsoft Graph
#Install-Module Microsoft.Graph
# Input Parameters
$clientId = '<client Id>'
$authTenant = '<Tenant Id>'
$graphScopes = "Directory.Read.all"
$clientSecret = '<client Secret Value>'
$tenantName = 'osinfotech.onmicrosoft.com'
$resource = "https://graph.microsoft.com/"
# Connect to Microsoft Graph
Connect-MgGraph -ClientId $clientId -TenantId $authTenant -Scopes $graphScopes -UseDeviceAuthentication
# About the Teams API filter with resourceProvisioningOptions
$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"
# Get the parameters form Microsoft Graph Teams
$tokenBody = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
Client_Id = $clientId
Client_Secret = $clientSecret
}
# Fetching the access token
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" -Method POST -Body $tokenBody
$result = Invoke-RestMethod -Headers @{Authorization = "Bearer $($tokenResponse.access_token)"} -Uri $URL -Method Get
($result | select-object Value).Value | Select-Object id, displayName, visibility, mail
# Create CSV file and add headings in CSV file
Add-Content -Path E:\PowerShellScripts\singleowner.csv -Value '"GroupID","TeamName","Visibility","Mail"'
# Export result into csv file
($result | select-object Value).Value | Select-Object id, displayName, visibility, mail | Export-Csv -Path E:\PowerShellScripts\singleowner.csv
- Run the script the output will show the Tenant Teams details.
Hope this article will help us to Access Microsoft Teams Graph API through PowerShell.
Also get my article updates on my social media handles.
LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/
Twitter – https://twitter.com/KhasimShaik2009
Facebook – https://www.facebook.com/profile.php?id=100078255554660
Thank you for your support, will catch up with new article soon.
Keep learning and keep smiling.
Thanks.
You must log in to post a comment.