Home
Add users to a Team using Powershell
Published: Wednesday, 18 March 2020 | Categories: Office 365 | Keywords: Powershell | Status: Posted | Created: Wednesday, 18 March 2020 | Author: 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
Param( 
    [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
Connect-MicrosoftTeams

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

# 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) {
   $upn=[string]$mb.UserPrincipalName.ToUpper()
   $tu=[string]$teamusers.User.ToUpper()
   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.