Add members to MS Teams as a bulk operation

--

Introduction

In this pandemic time period (I am writing this in 2020), social distancing is a vital thing in our day today life. But we are so used to collaborate with others and work as a team. All the companies, schools, universities are closed for months. But the life should go on.

With the current advancement of technology, online collaboration tools such as MS Teams and Zoom came to rescue us ❤.

In all these online collaboration tools, MS teams is the best one based on my opinion. There are several reasons and the most important one being able to create small sub teams and manage them separately. Adding assignments, marking them and seamless integration with other platforms is magical.

Problem

Even though I love MS Teams, one thing that always bothered me is the fact that it does not provide a way to add users to the team as a bulk operation. Once I was asked to create a team and add 280 students to the team. As I am the laziest person I know, it was a quite difficult and time consuming task

Solution

I spent about an hour researching a way to do this programmatically. And I found the PowerShell module for MS Teams. Since I had a small experience with batch scripting, I managed to use and tweak the commands and complete my task.

But I managed to create a PowerShell and batch script, in case I had to do it again. In this blog post I am sharing my script.

Prerequisites

  • In your search bar in Windows 10 or in your Start button, search for PowerShell. Select the Windows PowerShell app and right click and Run as administrator.
  • Then you need to install Microsoft Teams module in PowerShell. This allows us to manipulate teams with command lines. Type following command in your PowerShell window.

Install-Module -Name MicrosoftTeams

  • Command will prompt for installation confirmation. Select yes to all and proceed.
  • If you do not see any errors, you are good to go.
  • Then, we need to login to MS Teams using PowerShell.

Connect-MicrosoftTeams

  • This will popup an login window and you need to login with the correct teams account.

Running the Script

You need following information before running the script.

  • Group ID of the team.

You can run Get-Team command to find all the group IDs of your teams.

Else you can get the teams invite link from teams UI. You can find the group Id in the URL parameters.

  • Create a csv (comma separated values) file.

The simplest way to achieve this is to open a notepad and type following

email
abc@test.com
cde@test.com
fgh@test.com

then save that in .csv format. For this example I am going to save it as myemails.csv

  • Create a .bat file and paste the following code
@echo off

set /p GROUP_ID="Enter Group ID: "
set /p FILE_LOCATION="Enter the path to the emails csv file: "
set /p USER_TYPE="Enter the user type (Owner/Member): "

echo ID %GROUP_ID%
echo path %FILE_LOCATION%

Powershell.exe -Command "Import-Csv -Path '%FILE_LOCATION%' | foreach{Add-TeamUser -GroupId %GROUP_ID% -user $_.email -role %USER_TYPE%}"

pause
  • Then run the script

./script.bat

BUT…

If you are lazy like me, you would not be thrilled to install all these modules, login and all these process. For that I have created a PowerShell script.

  • Create a file with extension .ps1

script.ps1

  • Now copy the following code to that file.
Write-Host "Starting the script..."

$FILE_LOCATION = Read-Host -Prompt 'Enter the path to the emails csv file'
$GROUP_ID = Read-Host -Prompt 'Input Group ID'
$USER_TYPE = Read-Host -Prompt 'User Type(Owner/Member)'
$USER_EMAILS = Import-Csv -Path "$FILE_LOCATION"

if (Get-Module -ListAvailable -Name MicrosoftTeams) {
Write-Host "Microsoft Teams Module already Installed"
}
else {
try {
Write-Host "Installing Microsoft Teams Module"
Install-Module -Name MicrosoftTeams -AcceptLicense -AllowClobber -Confirm:$False -Force
}
catch [Exception] {
$_.message
exit
}
}

Connect-MicrosoftTeams

foreach ($email in $USER_EMAILS) {
Write-Host "--------------------------------------------------------------"
Write-Host ("Adding " + $email.email)
Add-TeamUser -GroupId $GROUP_ID -user $email.email -role $USER_TYPE
}

Write-Host " "
Write-Host " "
Write-Host "--------------------------------------------------------------"
Write-Host "Users added to the team successfully !"
Write-Host "--------------------------------------------------------------"

CMD /c PAUSE

If you are even lazier…

  • You have all the things you need in this git repository
  • see what I did there…🤣 ? Only the people read until the end can get the git repository. 😂😂😂

If you enjoyed this post and if you think this is valuable for others, please feel free to share this post. And please let me know if you have any ideas for automation.

Also Please consider adding a ⭐ to my repository.

Share this article :)

--

--

Gihan Siriwardhana
Microsoft Student Champs — Sri Lanka

Problem solver, Leader, Tech Enthusiast, Public Speaker and a student who seeks opportunities to enhance and share knowledge in any possible way.