add/remove dynamic member in microsoft teams

Iron Contributor

Hi

     How to write powershell to add/remove member in microsoft teams . I want add all user in OU to microsoft teams and also remove member if they not in this OU , please anyone guide help me

Best regards,

Thanks

6 Replies

Since Office 365/Azure AD does not have OUs, you will either have to export the list of OU users to a CSV file, or write a script that combines output from the AD and Office 365 modules. Here's a example for the former case:

 

$users = Get-UnifiedGroupLinks TeamName -LinkType member

Import-Csv .\blabla.csv | % { if ($_.UserPrincipalName -notin $users.WindowsLiveID) {Write-Host "Adding user $($_.UserPrincipalName)"; Add-UnifiedGroupLinks TeamName -LinkType member -Links $_.UserPrincipalName } else {Write-Host "Removing user $($_.UserPrincipalName)"; Remove-UnifiedGroupLinks TeamName -LinkType member -Links $_.UserPrincipalName} }

 

That's assuming the blabla.csv file has a column UserPrincipalName to designate the users (members of the OU).

hi

    I try add member but after done add member then microsoft teams update member but when access sharepoint site then see new member .Microsoft teams not update member i try logout and login Microsoft teams again but still not see

Best Regards,

You'll have to wait for the membership changes to be synchronized across all workloads.

I would like share my script just finish

thanks you @Vasil Michev help me

############## Add/Remove Member team base on AD Group ##############
$RootPath = "C:\ScriptsTest\AddRemoveMemberTeam"
$GroupName = "TestTeam-Group"
$TeamName = "Test-Calendar"

$Logfile = "$RootPath\outputlogs.csv"
Clear-Content $Logfile
############## Export Member AD Group to csv ############
Get-ADGroupMember -identity $GroupName | select @{name="UserPrincipalName";expression={$_.SamAccountName + "@test.com"}} | Export-csv -path $RootPath\Groupmembers.csv -NoTypeInformation

##############connect office365 ############

$NoTimeOut = New-PSSessionOption –IdleTimeout 3600000

$Username = "user@test.onmicrosoft.com"
$Password = ConvertTo-SecureString -AsPlainText "passwordpasthere" -Force

$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username,$Password
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic –AllowRedirection –SessionOption $NoTimeOut
Import-PSSession $session

############## Add/Remove ############

$TeamUsers = Get-UnifiedGroupLinks -Identity $TeamName -LinkType member
$GroupUsers = Import-Csv .\Groupmembers.csv

############## Add User from Group AD that no has member in Team ############
foreach($user in $GroupUsers)
{
if ($TeamUsers.WindowsLiveID -notcontains $user.UserPrincipalName)
{
Write-Host "Adding user " $user.UserPrincipalName -ForeGroundColor blue
Add-UnifiedGroupLinks -Identity $TeamName -LinkType member -Links $user.UserPrincipalName -Confirm:$false

$wrapper = New-Object PSObject -Property @{ Action='AddMember'; Username= $user.UserPrincipalName ; Group = $TeamName}
Export-Csv -InputObject $wrapper -Append -Force -Path $Logfile -NoTypeInformation
}
}

############## Remove User from Team that no has member in Group AD ############
foreach($user1 in $TeamUsers)
{
if ($GroupUsers.UserPrincipalName -notcontains $user1.WindowsLiveID)
{
Write-Host "Removing user " $user1.WindowsLiveID -ForeGroundColor Red
Remove-UnifiedGroupLinks -Identity $TeamName -LinkType member -Links $user1.WindowsLiveID -Confirm:$false

$wrapper = New-Object PSObject -Property @{ Action='RemoveMember'; Username= $user1.WindowsLiveID ; Group = $TeamName}
Export-Csv -InputObject $wrapper -Append -Force -Path $Logfile -NoTypeInformation
}
}

Remove-PSSession $Session

 

So the users did appear in the SPO membership as well, right?

@Vasil Michev  yes , after running scripts it have in SPO , but team need time to appear