Adding Users To Teams / Groups

Introduction

 
Recently our client has a requirement to add the users in bulk to Microsoft Teams. As a team owner, a user can add the users via Teams Interface. But if the users are in hundreds maybe in thousands, it becomes a tedious task. In such cases, there is a way to add the users in bulk via the PowerShell module. In this article let's see how we can add users in bulk. For this article, we will be adding only 5 users. The same script can be used for any no. of users. We are reading the users from a CSV file. 
 
Please note that when a team is created from the MS teams, at the backend there is an associated O365 group. For instance, I create a Teams called Contoso Planning, the corresponding O365 group is also called Contoso Planning.
 
Team Screenshot for 'Contoso Planning',
 
Adding Users to Teams / Groups
 
O365 Groups screenshot for 'Contoso Planning',
 
Adding Users to Teams / Groups
 
Adding Users to Teams / Groups
 
The associated email ID for the group Contoso Planning is ContosoPlanning@thecompanyname.onmicrosoft.com. Here I have replaced the actual domain name with 'thecompanyname'. We need this group email to add the users in bulk. 
 

Steps

 
Please note that we need to use an account that has exchange admin rights or global admin rights to run the following PowerShell script. Also, you need to have the latest version of the exchange online PS module installed. Please refer to the references section on how to install the latest Exchange Online Module. For this article the latest module is Exo-v2. 
 
Step 1
 
Connect to Exchange Online using exchange admin rights or global admin rights. 
  1. Connect-Exchangeonline
You will be asked to sign in. Use your Exchange Admin / Global Admin Account to log in
 
Adding Users to Teams / Groups
 
In the case of MFA, it will ask for a secondary code to sign in
 
Adding Users to Teams / Groups
 
Step 2 (Optional)
 
This step suppresses the email notification when adding users to the Teams. By default, system sends a welcome note to the user that has been added. But in some cases, the admin wants to turn off the notifications to avoid any confusion. They might want to send a global communication to all the users.  
  1. $GroupEmail = "ContosoPlanning@yourcompnay.onmicrosoft.com"  
  2. $GroupIdentity = Get-UnifiedGroup -Identity "ContosoPlanning@yourcompany.onmicrosoft.com" | select Identity    
  3. Set-UnifiedGroup -Identity $GroupIdentity.Identity -UnifiedGroupWelcomeMessageEnabled:$false   
Step 3
 
Get the List of users in CSV. Import the CSV to object and query on each email to get added to Group ID. If multiple group IDs are present, you may want to modify by writing multiple for each or adding multiple lines whichever is easier 
  1. Add-UnifiedGroupLinks -Identity $GroupEmail -LinkType Members -Links $email     
Step 4
 
Once done adding users you can toggle back welcome note.  
  1. Set-UnifiedGroup -Identity $GroupIdentity.Identity -UnifiedGroupWelcomeMessageEnabled:$false  
Step 5
 
Disconnect from Exchange Online Module  
  1. Disconnect-ExchangeOnline  -Confirm
You may be prompted with the message 'Are you sure you want to disconnect' select 'Yes'. 
 
Complete Script
  1. # Before running this, Please make sure you connect to Exchange Online Module.   
  2. #Below code if your account is not MFAed  
  3. Import-Module ExchangeOnlineManagement  
  4. Connect-ExchangeOnline    
  5. #PowerShell to Import Members to office 365 group from CSV  
  6. #Setting up variables  
  7.   
  8. $GroupEmail="ContosoPlanning@yourcompany.onmicrosoft.com"  
  9. $GroupIdentity=Get-UnifiedGroup -Identity $GroupEmail | select Identity  
  10. $currentTime = $(Get-Date).ToString("yyyymmddhhmmss");  
  11. $OutFile="C:\Temp\AddingUsers_Log-"+$currentTime+".csv"  
  12. $ExceptionLog= "C:\Temp\Exception_Log-"+$currentTime+".csv"  
  13. Add-Content -Path $OutFile -Value "Email,Status,Message"  
  14. try {  
  15. # Suppress Email Notification to the users  
  16. Set-UnifiedGroup $GroupIdentity.Identity -UnifiedGroupWelcomeMessageEnabled:$false -ErrorAction Stop   
  17. Write-Host "Welcome note disabled while adding..." -ForegroundColor Yellow  
  18. # Importing users and Adding users to the groups. Change the import path to your needs.   
  19. $UsersToAdd=Import-Csv "C:\Input\UserEmails.csv"  
  20.     foreach($email in $UsersToAdd.Email){        
  21.         try {  
  22.                           
  23.             Add-UnifiedGroupLinks -Identity $GroupIdentity.Identity -LinkType Members -Links $email -ErrorAction Stop  
  24.             #Write-Host "Added Member $email to the O365 group $GroupID.." -ForegroundColor Green  
  25.             $status="Success"  
  26.             $message="Added Member $email to the O365 group $GroupID"  
  27.             Add-Content -Path $OutFile -Value "$email,$status,$message"  
  28.             $status=$null  
  29.             $message=$null              
  30.         } catch {  
  31.             $status="Error"  
  32.             $message=$_.Exception.Message  
  33.             Add-Content -Path $OutFile -Value "$email,$status,$message"  
  34.         }  
  35.           
  36.     }  
  37.       
  38.     Write-Host "Completed adding users. Please check the logs." -ForegroundColor green  
  39.   
  40. #Reset the flag for welcome message  
  41. Set-UnifiedGroup $GroupIdentity.Identity -UnifiedGroupWelcomeMessageEnabled:$true   
  42. Write-Host "Reset the welcome note to the group.." -ForegroundColor Yellow  
  43.   
  44. Write-Host "Please disconnected from the exchange online module !!!" -ForegroundColor Yellow  
  45. }  
  46.   
  47. catch {  
  48.     Add-content -Path $ExceptionLog -Value $_.Exception.Message  
  49.     Write-Host "Error Occuured while adding user to group operations... Kindly check the exception logs at $ExceptionLog" -f $_.Exception.Message          
  50. }  
  51. finally {  
  52.     Disconnect-ExchangeOnline -Confirm  
  53. }  
In this script, the users are imported from the CSV file. Below is a screen capture of the CSV file, which is stored in C:\Input in this case. It is given in the format 
 
Adding Users to Teams / Groups
 
Output
 
Adding Users to Teams / Groups
Adding Users to Teams / Groups
 
Adding Users to Teams / Groups
 

Conclusion

 
Thus in this article, we have seen how to add users in bulk to O365 Groups. Please note that all the Teams permissions are referenced from backend O365 Group memberships. 
 
References
  • https://docs.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2?view=exchange-ps#install-and-maintain-the-exo-v2-module
  • https://jeffbrown.tech/using-exception-messages-with-try-catch-in-powershell