Add users to a Team using Powershell
Office 365 (Powershell) Wednesday, 18 March 2020 by paul

The powershell script below will add all users to a Microsoft Team as members. You will need to install the Microsoft Team powershell module to use it.

# Add users to a Microsoft Team
# Install-Module MicrosoftTeams

# Pass team name as parameter to script
    [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] 
    [string] $teamname

# Connect to Exchange powershell
if ($Session.state -eq 'Broken' -or !$Session) { 
	write-host "Connecting to Exchange Online Powershell.."
	$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential (Get-Credential) -Authentication Basic -AllowRedirection
	Import-PSSession $Session

# Login and Connect to Microsoft Teams

# Get-Team |Select GroupId, DisplayName
$team = get-team -displayname $teamname

# Get users already in team
$teamusers = $team | get-teamuser | select user

# Get all user mailboxes to add
$mailboxes = get-mailbox -Resultsize unlimited -Filter {Recipienttypedetails -eq "UserMailbox" -and IsInactiveMailbox -eq "False"} | select UserPrincipalName
# Alternatively import users from csv file
# $mailboxes = import-csv "users-to-add.csv"

foreach ($mb in $mailboxes) {
   Write-host "Checking"$upn
   If ($tu.Contains($upn)) {
      # user already in team. do nothing
      Write-host "$upn already in team $teamname" -foreground green
   } else {
      # user not in team. add user as member
      Write-host "Adding $upn to $teamname" -foreground red
      Add-TeamUser -User $upn -GroupId $team.GroupId -Role Member

Only an example. An Org-Wide team may be a better option than adding users using a script.

Add Comment
No Comments.