Entra / Microsoft 365 · Teams
Populate team shared channel
Populate the membership of a Teams shared channel with every licensed user account in the tenant.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes User.Read.All
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
$Modules = @( "MicrosoftTeams", "Microsoft.Graph" )# Requires -Modules $Modules$ModulesLoaded = Get-Module | Select NameIf (!($ModulesLoaded -match "MicrosoftTeams")) {Write-Host "Please connect to the Microsoft Teams module and then restart the script"; break}Connect-MgGraph -Scopes User.Read.AllSelect-MgProfile Beta# Find user accounts with licenses[array]$Users = Get-MgUser -Filter "assignedLicenses/`$count ne 0 and userType eq 'Member'" -ConsistencyLevel eventual -CountVariable Records -AllIf (!($Users)) { Write-Host "No user accounts found - exiting" ; break }# Filter out any accounts marked that shouldn't be added to team membership$FilteredUsers = $Users | ? {$_.OfficeLocation -ne "XXX"}# Now check that each user actually has a Teams service plan$UsersWithTeams = [System.Collections.Generic.List[Object]]::new()ForEach ($User in $FilteredUsers) {$TeamsLicense = Get-MgUserLicenseDetail -UserId $User.Id | Select-Object -ExpandProperty ServicePlans | ? {$_.ServicePlanId -eq "57ff2da0-773e-42df-b2af-ffb7a2317929"} | Select-Object -ExpandProperty ProvisioningStatusIf ($TeamsLicense -eq "Success") {$UserData = [PSCustomObject][Ordered]@{ # Write out details of the userId = $User.IdDisplayName = $User.DisplayName }$UsersWithTeams.Add($UserData)} #End if} #End ForEach# These variables will differ depending on the host team and channel name you decide to use$GroupId = (Get-Team -DisplayName "HR Questions and Answers").GroupId$ChannelName = "Questions and Answers"Write-Host ("Finding the membership of of the {0} channel" -f $ChannelName)# Find current members and owners and add them to a hash table that we can lookup$ChannelMembers = Get-TeamChannelUser -GroupId $GroupId -DisplayName $ChannelName -Role Member$ChannelOwners = Get-TeamChannelUser -GroupId $GroupId -DisplayName $ChannelName -Role Owner$CurrentMembers = @{}ForEach ($Member in $ChannelMembers) {$CurrentMembers.Add($Member.UserId,$Member.User) }ForEach ($Member in $ChannelOwners) {$CurrentMembers.Add($Member.UserId,$Member.User) }$i = 0# Check each user and add them if they are not foundForEach ($User in $UsersWithTeams) {If (!($CurrentMembers[$User.Id])) {Write-Host ("Adding {0} to the {1} channel" -f $User.DisplayName, $ChannelName)Add-TeamChannelUser -GroupId $GroupId -DisplayName $ChannelName -User $User.Id; $i++ }}Write-Host ("{0} new members added to channel" -f $i)
Attribution
Author
Office365itpros