Nieuw Office 365 bericht: How to bulk add users to a MS Team

Sometimes you want to bulk add users to a team. There is an interface to add users but you have to add them one by one:

In some cases you want to add a lot of users. For example you are a teacher and wants to add your students before the start of your classes. Doing this one by one will be very time consuming. So I created a PowerShell script to do this. You don’t need any special permissions to run the script. You only have to be the owner of the team.

First we need a few things:

The teams group Id

Every team (or group) has an unique Id. You can find this by getting the link of the team and search for the groupId parameter in the url:

The url is something like this: (fake url for security reasons ? )

You need to copy the id: e000eb72-0948-4a89-b1ad-ad708beebbb4

Next we need a list with email addresses that you want to add. Store them in a plain text file (input.txt)

The script will use Microsoft Teams and AzureAD commands so make sure you have installed them by running these commands in PowerShell once (open the PowerShell terminal with elevated permissions):

Install-Module -Name MicrosoftTeams 
Install-Module -Name AzureAD

Now we can start, run the following script. You’ll have to enter your credentials a few times.

#change the path to the text file:
$emailaddresses=Get-Content -Path "C:\Users\demouser\input.txt"

#change the ID parameter here
$teamId="GROUP - ID"

# -----------------------------------------
# Run this first
# -----------------------------------------
Set-ExecutionPolicy RemoteSigned
# -----------------------------------------

#not working with MFA accounts

Connect-MicrosoftTeams #-Credential $crendentials
Connect-AzureAD #-Credential $crendentials

$team=Get-Team -GroupId $teamId

$groupMembers=(Get-AzureADGroupMember -ObjectId $teamId -All $true | select UserPrincipalName)

#Write-Host $groupMembers

Write-Host "Check current users" -ForegroundColor White
for($i=0; $i -lt $groupMembers.length; $i++){
    write-host $groupMembers[$i]

    #check if user is in valid user list
    #if NO, then delete the user from TEAMS

        #Write-Host "user found"
        Write-Host "user not found: remove from TEAMS" -ForegroundColor Red
        Remove-TeamUser -groupId $teamId -User $groupMembers[$i].UserPrincipalName

#add new users
Write-Host "Check user membership" -ForegroundColor White
for($i=0; $i -lt $emailaddresses.length; $i++){
    write-host $emailaddresses[$i]
    #check if the user is already in TEAMS
    #if NO, then ADD the user
    for($j=0; $j -lt $groupMembers.length;$j++){
        if($groupMembers[$j].UserPrincipalName.toLower() -eq $emailaddresses[$i].ToLower()){
            #Write-Host "user found in teams members list" -ForegroundColor Yellow

        Write-Host "adding member to TEAMS" -ForegroundColor Green
        Add-TeamUser -GroupId $teamId -User $emailaddresses[$i]


Feel free to use this!

Dit verscheen eerst op

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *