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.com/?s=microsoft+graph.

Microsoft Graph
M365: 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.
Azure Active Directory - App registrations
M365: Azure Active Directory – App registrations
  • Click New Registration.
Active Directory - New registration
M365: Active Directory – New registration
  • Enter the Name and click Register.
Azure Directory - Register an application
M365: Azure Directory – Register an application
  • App is registered successfully. In the left navigation, click API Permissions under Manage.
Azure Directory - API permissions
M365: Azure Directory – API permissions
  • Click Add a permission.
Azure Directory - Add a permission
M365: Azure Directory – Add a permission
  • Select Microsoft Graph API as shown below.
Azure Directory - Select Microsoft Graph
M365: Azure Directory – Select Microsoft Graph
  • Click Application Permissions.
Azure Directory - Application permissions
M365: Azure Directory – Application permissions
  • Select Directory.Read.All permissions and click Add permissions.
Azure Directory - Add permissions
M365: Azure Directory – Add permissions
  • Click Grant admin consent.
Azure Directory - Grant admin consent
M365: Azure Directory – Grant admin consent
Azure Directory - permissions added
M365: Azure Directory – permissions added
  • In the left navigation, click Overview. Copy the Application (client) ID value. This value will be used in PowerShell for authentication.
Azure Directory - copy Application ID
M365: Azure Directory – copy Application ID
  • In the left navigation, click Certificates & secrets under Manage. Click New client secret.
Azure Directory - Certificates and secrets
M365: Azure Directory – Certificates and secrets
  • Enter the description and click Add.
Azure Directory - Add a client secret
M365: Azure Directory – Add a client secret
  • Copy the secret value which will be used in PowerShell for authentication.
Azure Directory - copy Secret Value
M365: Azure Directory – copy Secret Value

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.

Khasim Shaik

SharePoint & Power Platform Developer at OS InfoTech

You may also like...

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

%d bloggers like this: