add/remove dynamic member in microsoft teams

%3CLINGO-SUB%20id%3D%22lingo-sub-957639%22%20slang%3D%22en-US%22%3Eadd%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-957639%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20How%20to%20write%20powershell%20to%20add%2Fremove%20member%20in%20microsoft%20teams%20.%20I%20want%20add%20all%20user%20in%20OU%20to%20microsoft%20teams%20and%20also%20remove%20member%20if%20they%20not%20in%20this%20OU%20%2C%20please%20anyone%20guide%20help%20me%3C%2FP%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-957639%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPI%20Management%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Active%20Directory%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-957688%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-957688%22%20slang%3D%22en-US%22%3E%3CP%3ESince%20Office%20365%2FAzure%20AD%20does%20not%20have%20OUs%2C%20you%20will%20either%20have%20to%20export%20the%20list%20of%20OU%20users%20to%20a%20CSV%20file%2C%20or%20write%20a%20script%20that%20combines%20output%20from%20the%20AD%20and%20Office%20365%20modules.%20Here's%20a%20example%20for%20the%20former%20case%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%24users%20%3D%20Get-UnifiedGroupLinks%20TeamName%20-LinkType%20member%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EImport-Csv%20.%5Cblabla.csv%20%7C%20%25%20%7B%20if%20(%24_.UserPrincipalName%20-notin%20%24users.WindowsLiveID)%20%7BWrite-Host%20%22Adding%20user%20%24(%24_.UserPrincipalName)%22%3B%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EAdd-UnifiedGroupLinks%20TeamName%20-LinkType%20member%20-Links%20%24_.UserPrincipalName%20%3C%2FFONT%3E%7D%20else%20%7B%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-family%3A%20%26amp%3Bquot%3B%20segoeui%26amp%3Bquot%3B%2C%26amp%3Bquot%3Blato%26amp%3Bquot%3B%2C%26amp%3Bquot%3Bhelvetica%20neue%26amp%3Bquot%3B%2Chelvetica%2Carial%2Csans-serif%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3EWrite-Host%20%22Removing%20user%20%24(%24_.UserPrincipalName)%22%3B%20%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%20box-sizing%3A%20border-box%3B%20color%3A%20%23333333%3B%20font-size%3A%2016px%3B%20font-style%3A%20normal%3B%20font-variant%3A%20normal%3B%20font-weight%3A%20300%3B%20letter-spacing%3A%20normal%3B%20orphans%3A%202%3B%20text-align%3A%20left%3B%20text-decoration%3A%20none%3B%20text-indent%3A%200px%3B%20text-transform%3A%20none%3B%20-webkit-text-stroke-width%3A%200px%3B%20white-space%3A%20normal%3B%20word-spacing%3A%200px%3B%22%3ERemove-UnifiedGroupLinks%20TeamName%20-LinkType%20member%20-Links%20%24_.UserPrincipalName%3C%2FFONT%3E%3C%2FFONT%3E%7D%20%7D%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EThat's%20assuming%20the%20blabla.csv%20file%20has%20a%20column%20UserPrincipalName%20to%20designate%20the%20users%20(members%20of%20the%20OU).%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-971124%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-971124%22%20slang%3D%22en-US%22%3E%3CP%3Ehi%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20I%20try%20add%20member%20but%20after%20done%20add%20member%20then%20microsoft%20teams%20update%20member%20but%20when%20access%20sharepoint%20site%20then%20see%20new%20member%20.Microsoft%20teams%20not%20update%20member%20i%20try%20logout%20and%20login%20Microsoft%20teams%20again%20but%20still%20not%20see%3C%2FP%3E%3CP%3EBest%20Regards%2C%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-974240%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-974240%22%20slang%3D%22en-US%22%3E%3CP%3EYou'll%20have%20to%20wait%20for%20the%20membership%20changes%20to%20be%20synchronized%20across%20all%20workloads.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-976055%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-976055%22%20slang%3D%22en-US%22%3E%3CP%3EI%20would%20like%20share%20my%20script%20just%20finish%3C%2FP%3E%3CP%3Ethanks%20you%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%20help%20me%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Add%2FRemove%20Member%20team%20base%20on%20AD%20Group%20%23%23%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3E%24RootPath%20%3D%20%22C%3A%5CScriptsTest%5CAddRemoveMemberTeam%22%3CBR%20%2F%3E%24GroupName%20%3D%20%22TestTeam-Group%22%3CBR%20%2F%3E%24TeamName%20%3D%20%22Test-Calendar%22%3C%2FP%3E%3CP%3E%24Logfile%20%3D%20%22%24RootPath%5Coutputlogs.csv%22%3CBR%20%2F%3EClear-Content%20%24Logfile%3CBR%20%2F%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Export%20Member%20AD%20Group%20to%20csv%20%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3EGet-ADGroupMember%20-identity%20%24GroupName%20%7C%20select%20%40%7Bname%3D%22UserPrincipalName%22%3Bexpression%3D%7B%24_.SamAccountName%20%2B%20%22%40test.com%22%7D%7D%20%7C%20Export-csv%20-path%20%24RootPath%5CGroupmembers.csv%20-NoTypeInformation%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23connect%20office365%20%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3E%24NoTimeOut%20%3D%20New-PSSessionOption%20%E2%80%93IdleTimeout%203600000%3C%2FP%3E%3CP%3E%24Username%20%3D%20%22user%40test.onmicrosoft.com%22%3CBR%20%2F%3E%24Password%20%3D%20ConvertTo-SecureString%20-AsPlainText%20%22passwordpasthere%22%20-Force%3C%2FP%3E%3CP%3E%24cred%20%3D%20new-object%20-typename%20System.Management.Automation.PSCredential%20-argumentlist%20%24Username%2C%24Password%3CBR%20%2F%3E%24Session%20%3D%20New-PSSession%20-ConfigurationName%20Microsoft.Exchange%20-ConnectionUri%20%3CA%20href%3D%22https%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Foutlook.office365.com%2Fpowershell-liveid%2F%3C%2FA%3E%20-Credential%20%24cred%20-Authentication%20Basic%20%E2%80%93AllowRedirection%20%E2%80%93SessionOption%20%24NoTimeOut%3CBR%20%2F%3EImport-PSSession%20%24session%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Add%2FRemove%20%23%23%23%23%23%23%23%23%23%23%23%23%3C%2FP%3E%3CP%3E%24TeamUsers%20%3D%20Get-UnifiedGroupLinks%20-Identity%20%24TeamName%20-LinkType%20member%3CBR%20%2F%3E%24GroupUsers%20%3D%20Import-Csv%20.%5CGroupmembers.csv%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Add%20User%20from%20Group%20AD%20that%20no%20has%20member%20in%20Team%20%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3Eforeach(%24user%20in%20%24GroupUsers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(%24TeamUsers.WindowsLiveID%20-notcontains%20%24user.UserPrincipalName)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20%22Adding%20user%20%22%20%24user.UserPrincipalName%20-ForeGroundColor%20blue%3CBR%20%2F%3EAdd-UnifiedGroupLinks%20-Identity%20%24TeamName%20-LinkType%20member%20-Links%20%24user.UserPrincipalName%20-Confirm%3A%24false%3CBR%20%2F%3E%3CBR%20%2F%3E%24wrapper%20%3D%20New-Object%20PSObject%20-Property%20%40%7B%20Action%3D'AddMember'%3B%20Username%3D%20%24user.UserPrincipalName%20%3B%20Group%20%3D%20%24TeamName%7D%3CBR%20%2F%3EExport-Csv%20-InputObject%20%24wrapper%20-Append%20-Force%20-Path%20%24Logfile%20-NoTypeInformation%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%23%23%23%23%23%23%23%23%23%23%23%23%23%23%20Remove%20User%20from%20Team%20that%20no%20has%20member%20in%20Group%20AD%20%23%23%23%23%23%23%23%23%23%23%23%23%3CBR%20%2F%3Eforeach(%24user1%20in%20%24TeamUsers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3Eif%20(%24GroupUsers.UserPrincipalName%20-notcontains%20%24user1.WindowsLiveID)%3CBR%20%2F%3E%7B%3CBR%20%2F%3EWrite-Host%20%22Removing%20user%20%22%20%24user1.WindowsLiveID%20-ForeGroundColor%20Red%3CBR%20%2F%3ERemove-UnifiedGroupLinks%20-Identity%20%24TeamName%20-LinkType%20member%20-Links%20%24user1.WindowsLiveID%20-Confirm%3A%24false%3C%2FP%3E%3CP%3E%24wrapper%20%3D%20New-Object%20PSObject%20-Property%20%40%7B%20Action%3D'RemoveMember'%3B%20Username%3D%20%24user1.WindowsLiveID%20%3B%20Group%20%3D%20%24TeamName%7D%3CBR%20%2F%3EExport-Csv%20-InputObject%20%24wrapper%20-Append%20-Force%20-Path%20%24Logfile%20-NoTypeInformation%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%3CBR%20%2F%3ERemove-PSSession%20%24Session%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-976707%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-976707%22%20slang%3D%22en-US%22%3E%3CP%3ESo%20the%20users%20did%20appear%20in%20the%20SPO%20membership%20as%20well%2C%20right%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-983803%22%20slang%3D%22en-US%22%3ERe%3A%20add%2Fremove%20dynamic%20member%20in%20microsoft%20teams%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-983803%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%20yes%20%2C%20after%20running%20scripts%20it%20have%20in%20SPO%20%2C%20but%20team%20need%20time%20to%20appear%3C%2FP%3E%3C%2FLINGO-BODY%3E
Regular 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