Add Bulk Users to Microsoft Teams from CSV using PowerShell

Microsoft Teams provides a collaborative workspace environment within Microsoft 365. Users can use the Teams as a central hub for group conversations, team meetings, and document sharing. In this post, we will explore how to add members and owners to Teams in bulk using PowerShell.

We can use the Add-TeamUser cmdlet to add a user as a member or owner in a team. Install the latest Microsoft Teams PowerShell module and run the following command to connect the Teams module.

Connect-MicrosoftTeams

Add Member to a Team

Use the below command to add a user as a member to the Team. This command also adds the user to the unified group (Microsoft 365 Groups) which backs the team.

#Syntax
Add-TeamUser -GroupId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -User "[email protected]"

#Get team details by display name
$TeamObj = Get-Team -DisplayName "TestTeam1"
#Add user to the team
Add-TeamUser -GroupId $TeamObj.GroupId -User "[email protected]"

Add user as Owner to a Team

We can use the same command to add the owner by adding the parameter Role as Owner.

$TeamObj = Get-Team -DisplayName "TestTeam1"
Add-TeamUser -GroupId $TeamObj.GroupId -User "[email protected]" -Role Owner

Import Bulk Users from CSV and Add to Multiple Teams

You can use the above commands to add a single user as a member or owner into a single Team. In a large environment, we may need to add members and owners in bulk into multiple teams. Consider the CSV file “TeamUsers.csv” (Download sample CSV) which holds the team and user identity values in each row with the column headers TeamNameGroupIDUserID, and UserPrincipalName. The below script imports the team users and adds the users one by one into the given team (GroupId). 

#Read team users from CSV file
$TeamUsers = Import-CSV "C:\Temp\TeamUsers.csv"
$i = 0;
$TotalRows = $TeamUsers.Count
#Iterate team user details one by one
Foreach($TeamUser in $TeamUsers)
{
$TeamName = $TeamUser.'TeamName'
$GroupId = $TeamUser."GroupID"
$UserId = $TeamUser.'UserPrincipalName'
$Role = $TeamUser.'Role'

$i++;
Write-Progress -activity "Processing $TeamName - $UserId - $Role" -status "$i out of $TotalRows completed"
Try
{
#Add user to the team
Add-TeamUser -GroupId $GroupId -User $UserId -Role $Role
}
catch
{
Write-Host "Error occurred for $TeamName - $UserId" -f Yellow
Write-Host $_ -f Red
}
}

List Team Members and Export to CSV

Once you successfully added the users to the required Team, we can use the Get-TeamUser cmdlet to retrieve members and owners of the team.

$TeamObj = Get-Team -DisplayName "TestTeam1"
$TeamMembers = Get-TeamUser -GroupId $TeamObj.GroupId 

#List the members
$TeamMembers

#Export the members
$TeamMembers | Export-CSV "C:\Temp\TeamMembers.CSV" -NoTypeInformation -Encoding UTF8
Advertisement

Leave a Comment