Adding multiple users to a Microsoft Teams group manually can be tedious and error-prone, especially for large groups. As we use Teams to manage class, we need to add upto 35 students for each sessional class, 65 students for theory and 195 students if we want to add the entire batch in a team. Fortunately, we can automate this task using PowerShell simplifies the task, reducing time and mistakes. Also, it is not straight forward to export the list of members from a team to a Excel file.
I am releasing these two templates to make our lives easier:
In this tutorial, you’ll learn how to use PowerShell and Excel to bulk-add users to a Microsoft Teams group. You can also download the templates and directly use them:
BulkAdd.xlsx
) containing users’ email addresses and roles.Windows typically comes with PowerShell pre-installed. To open it:
If you need to install the latest PowerShell version, download it here.
Download and install PowerShell from the official GitHub repository:
After installing, open the terminal and type pwsh
to launch PowerShell.
You need two modules: MicrosoftTeams
and ImportExcel
.
Open PowerShell as Administrator and run:
Install-Module MicrosoftTeams -Scope CurrentUser
Install-Module ImportExcel -Scope CurrentUser
Download Excel Template for Bulk Adding Team Members
In the Data
sheet, enter Batch, Department and Section. The roll number range should be automatically generated for departments with 195 students, and for other departments, you need to tweak the Sections
sheet.
Here’s the script that automates the bulk addition of users:
Download PowerShellScript for Bulk Adding Team Members
Unzip the file and extract the Teams-bulk-add.ps1
file, keep it in same folder as the excel file.
Modify the TeamDisplayName
variable as your team’s name:
# Define Team display name
$TeamDisplayName = "EEE 416 (Jan 2025) B1 + B2"
# Ensure ImportExcel module is installed
if (!(Get-Module -ListAvailable -Name ImportExcel)) {
Write-Host "ImportExcel module not found, installing..."
Install-Module ImportExcel -Scope CurrentUser -Force
} else {
Write-Host "ImportExcel module already installed."
}
# Ensure MicrosoftTeams module is installed
if (!(Get-Module -ListAvailable -Name MicrosoftTeams)) {
Write-Host "MicrosoftTeams module not found, installing..."
Install-Module MicrosoftTeams -Scope CurrentUser -Force
} else {
Write-Host "MicrosoftTeams module already installed."
}
# Import required modules
Import-Module ImportExcel
Import-Module MicrosoftTeams
# Connect to Microsoft Teams
Write-Host "Connecting to Microsoft Teams..."
Connect-MicrosoftTeams
# Get Team Group ID
$team = Get-Team -DisplayName $TeamDisplayName
if ($null -eq $team) {
Write-Host "No team found with display name '$TeamDisplayName'. Exiting script."
exit
}
$groupId = $team.GroupId
Write-Host "Team '$TeamDisplayName' found. Group ID: $groupId"
# Read data from BulkAdd.xlsx (assuming file is in the same folder as script)
$scriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$excelPath = Join-Path $scriptPath "BulkAdd.xlsx"
if (!(Test-Path $excelPath)) {
Write-Host "File 'BulkAdd.xlsx' not found in script directory. Exiting script."
exit
}
# Read the Excel sheet named "PowerShellData"
$data = Import-Excel -Path $excelPath -WorksheetName "PowerShellData"
Write-Host "Debugging imported data:"
$data | Format-Table
foreach ($row in $data) {
$email = $row | Select-Object -ExpandProperty (Get-Member -InputObject $row -MemberType NoteProperty | Select-Object -First 1 -ExpandProperty Name)
$role = $row | Select-Object -ExpandProperty (Get-Member -InputObject $row -MemberType NoteProperty | Select-Object -Skip 1 -First 1 -ExpandProperty Name)
if ([string]::IsNullOrEmpty($email) -or [string]::IsNullOrEmpty($role)) {
Write-Host "Empty email or role found. Skipping..."
break
}
Write-Host "Email: $email, Role: $role"
#Add users to team (uncomment the following line to add users to the team)
Add-TeamUser -GroupId $groupId -User $email -Role $role
}
Write-Host "Process completed."
Replace Your Team Display Name
with your actual Microsoft Teams name where you want to add the members.
Save this script as Teams-bulk-add.ps1
. Run it by opening PowerShell and executing:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
.\Teams-bulk-add.ps1
If prompted about execution policy, type Y
and hit Enter.
Log into Microsoft Teams after Running the script
Here is a screenshot of the script adding members to the team
Here is a screenshot of the teams after all 65 members added.
Download PowerShellScript for Exporting Team Members to Excel
Modify the TeamDisplayName
variable as your team’s name.
1# Define Team display name
2$TeamDisplayName = "EEE 416 (Jan 2025) B1 + B2"
3
4# Connect to Microsoft Teams
5Write-Host "Connecting to Microsoft Teams..."
6Connect-MicrosoftTeams
7
8# Get Team Group ID
9$selectedTeam = Get-Team -DisplayName $TeamDisplayName
10
11if ($null -eq $selectedTeam) {
12 Write-Host "No team found with display name '$TeamDisplayName'. Exiting script."
13 exit
14}
15
16
17
18if (-not $selectedTeam) {
19 Write-Error "Team '$teamName' not found. Exiting script."
20 exit
21}
22
23# Get the members of the selected Team
24$members = Get-TeamUser -GroupId $selectedTeam.GroupId
25
26# Sort members by Email Address
27$sortedMembers = $members | Sort-Object User
28
29# Prepare data
30$memberDetails = foreach ($member in $sortedMembers) {
31 [PSCustomObject]@{
32 "Display Name" = $member.Name
33 "Email Address" = $member.User
34 "Role" = $member.Role
35 }
36}
37
38# Install ImportExcel Module if not already installed
39if (-not (Get-Module -ListAvailable -Name ImportExcel)) {
40 Install-Module -Name ImportExcel -Scope CurrentUser -Force
41}
42
43# Import the ImportExcel module
44Import-Module ImportExcel
45
46# Define Excel file name
47$excelFileName = ($selectedTeam.DisplayName -replace ' ','_') + "_TeamMembers.xlsx"
48
49# Export to Excel
50$memberDetails | Export-Excel -Path $excelFileName -AutoSize -WorksheetName "Team Members"
51
52Write-Host "Team members exported successfully to $excelFileName" -ForegroundColor Green
Run the script
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
.\Teams-getmembers.ps1
The script would create an excel file with all team members.
You’ve now automated the bulk addition of users into Microsoft Teams using PowerShell. This approach saves considerable time, especially when managing large teams.
Happy automating!
Note: I might update the script in the future, where it would check against the BIIS generated roll number list, and also automatically remove members that are not present in the BIIS list, but that might need further testing.