Automating Microsoft Teams Shared Channel Membership with PowerShell

5 min. readlast update: 10.13.2024

Managing shared channels and user memberships in Microsoft Teams can become a complex task, especially when dealing with large groups of users across multiple teams. This PowerShell script automates the process of adding users to shared channels in Microsoft Teams using data from a CSV file and the Microsoft Graph API.

In this article, we will explore the script’s functionality, which efficiently adds members to shared channels based on information retrieved from Microsoft Graph and a CSV file containing team and channel details.

Script Overview

This script performs the following tasks:

  1. Connects to Microsoft Graph to retrieve user data.
  2. Reads user and channel details from a CSV file to map users to specific shared channels.
  3. Adds users to shared channels in Microsoft Teams, automating a process that can otherwise be time-consuming and prone to errors when done manually.

Requirements:

Before running the script, ensure you have:

  • The Microsoft Graph PowerShell SDK installed.
  • Appropriate permissions to Microsoft Graph (e.g., User.ReadBasic.All).
  • A CSV file containing the required fields: Member Email, Team ID, Team Name, Shared Channel Name, and User ID.

PowerShell Script Breakdown

1. Connecting to Microsoft Graph

The script begins by establishing a connection to Microsoft Graph with the required scope (User.ReadBasic.All) to retrieve basic user details from the tenant.

 
Connect-MgGraph -Scopes User.ReadBasic.All

This command allows the script to access user information and is necessary for mapping the CSV data to actual users in the Microsoft 365 environment.

2. Retrieving Users from Microsoft Graph

To optimize performance, the script retrieves all users in a single call and stores their information in a hashtable for fast lookups. The hashtable maps the UserPrincipalName to the user’s details.

$Users = @{} Get-MgUser -All -Property DisplayName,Id,Userprincipalname,Mail,UserType -Filter "UserType eq 'Member'" | ForEach-Object { $user = $_ $Users[$user.Userprincipalname] = $user }

This approach reduces the need for multiple API calls, improving the efficiency of the script.

3. Reading the CSV File

The script then imports the CSV file that contains the information about users, teams, and shared channels.

$csvPath = "$home\Desktop\DetailedPM.csv" $csv = Import-Csv -Path $csvPath

The CSV file is expected to contain the following fields:

  • Member Email: The email address of the user to be added.
  • Team ID: The ID of the team where the shared channel exists.
  • Team Name: The name of the team (for informational purposes).
  • Shared Channel Name: The name of the shared channel.
  • User ID: The current user ID associated with the channel.

4. Adding Users to Shared Channels

For each entry in the CSV file, the script checks if the user exists in the tenant by looking them up in the hashtable. If the user is found and their User ID in the CSV differs from their actual Microsoft Graph User ID, they are added to the shared channel using Add-TeamChannelUser.

 
$csv | ForEach-Object { $Mail = $_.'Member Email' $UserId = $_.UserId if ($Users.ContainsKey($Mail)) { $UserToAdd = $Users[$Mail] if ($UserToAdd.Id -ne $UserId) { Write-Host "Adding $($UserToAdd.Userprincipalname) to $($_.'Team Name') \ $($_.'Shared Channel Name')..." Add-TeamChannelUser -GroupId $_.TeamId -DisplayName $_.'Shared Channel Name' -User $UserToAdd.Userprincipalname } else { Write-Host "Skip: $Mail is the same as the existing user with Id $UserId." } } else { Write-Host "User $Mail not found in the Microsoft Graph." } }
  • Check for Existing User: If the user is already a member of the shared channel (i.e., their User ID matches the one in the CSV file), the script skips the user to avoid duplication.
  • Add User: If the user is found and they are not currently a member of the shared channel, the script adds them to the channel.

5. Error Handling

The script handles errors gracefully, informing the administrator when users are not found in Microsoft Graph or when there are issues during the process of adding users.

CSV File Structure

The CSV file is a critical part of this process. Ensure the file includes the following fields for each user:

Member Email Team ID Team Name Shared Channel Name User ID
user1@domain.com abc123 Sales Team Sales Updates 12345678
user2@domain.com def456 Marketing Team Campaign Discussions 87654321

Best Practices for Running the Script:

  • Test Before Running in Production: Always run the script in a test environment or with a small subset of users to ensure it behaves as expected.
  • Ensure Correct Permissions: Verify that the user running the script has the necessary permissions in both Microsoft Graph and Microsoft Teams to manage users and shared channels.
  • Backup Membership Data: Before making changes to channel memberships, it’s a good idea to export the current membership data to a CSV file as a backup.

Why Automate Team Membership Management?

Manually adding users to Microsoft Teams shared channels can be tedious, especially in larger organizations with many teams and channels. Automating this process ensures that users are added accurately and efficiently, reducing the likelihood of human error while saving significant time.

Conclusion

This PowerShell script offers a streamlined approach to managing Microsoft Teams shared channel memberships by integrating with Microsoft Graph and using data from a CSV file. It ensures that users are added to the correct shared channels based on real-time data, providing administrators with a powerful tool for automating team membership management.

By integrating this script into your workflow, you can automate the process of adding users to shared channels, saving time and ensuring accurate team collaboration.

Was this article helpful?