Had to bulk add a user to multiple Teams and channels today so spent some time with Teams PowerShell.
This cmdlet lists all the teams a user is member of.
1 |
Get-Team -User "abc@def.com" |
The output gives the group ID (GroupId
) which is what you need for most other cmdlets. This is the group ID of the O365 group backing the team. You also get the Display Name and other details.
To find the channels in a team:
1 |
Get-TeamChannel -GroupId "<group Id>" |
The output includes the Display Name which can be used for channel related options. Not shown by default, but MembershipType
has the channel type.
To find the members of a team:
1 |
Get-TeamUser -GroupId "<group Id>" |
I wanted to add a user to all (private) channels of a team:
1 2 3 |
Get-TeamChannel -GroupId "<group Id>" | Where-Object { $_.MembershipType -eq "Private" } | ForEach-Object { Add-TeamChannelUser -GroupId "<same group Id>" -DisplayName $_.DisplayName -User "abc@def.com" -Role Owner } |
That didn’t work for some reason. Kept throwing a “Could not find member
.” error. Not many (or any!) Google hits on that error either.
1 2 3 4 5 6 7 |
Add-TeamChannelUser: Error occurred while executing Code: NotFound Message: Could not find member. InnerError: RequestId: 9d0b00e5-1a90-44d9-a840-f5dcd86e6dfb DateTimeStamp: 2023-01-17T18:29:55 HttpStatusCode: NotFound |
Tried updating the module but that didn’t help either. Tried using User Id instead of UPN, no luck.
Guess what finally worked! I ran the cmdlet without the -Role
switch, it succeeded, and then I re-ran with the -Role Owner
switch. I wanted to set the user as Owner so that’s why I had the -Role Owner
switch, but that’s optional if you want to add the user as a regular member.
Weird.
To get all the teams and channels of a user:
1 2 3 4 5 6 7 8 9 |
$outputObjArray = [System.Collections.ArrayList]@() Get-Team -user "abc@def.com" | ForEach-Object { $OutputHash = [ordered]@{ "Team" = (Get-Team -GroupId $_.GroupId).DisplayName "GroupId" = $_.GroupId "Channels" = (Get-TeamChannel -GroupId $_.GroupId | Where-Object { $_.MembershipType -eq "Private" }).DisplayName -join '; ' } $outputObjArray.Add($OutputHash) | Out-Null } |
I had this weird issue today wherein we moved a user across forests and while that preserves the Azure AD account and group memberships and Get-Team
even shows the user as belonging to all his teams he wasn’t seeing any of them in Teams itself. I noticed that changing the user from member to owner or owner to member starts showing the team.
I want to do this via PowerShell, but only for Teams that don’t have any private channels (coz I noticed he is removed from them and I wonder if it’s got anything to do with the flipping around; so I want to handle them separately).
To get all teams of a user without private channels (or no channels):
1 2 3 4 |
Get-Team -user "abc@def.com" | ForEach-Object { $privateChannels = Get-TeamChannel -GroupId $_.GroupId | Where-Object { $_.MembershipType -eq "Private" } if ($privateChannels.Count -eq 0) { (Get-Team -GroupId $_.GroupId).DisplayName } } |
Now to build upon that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$user = "abc@def.com" Get-Team -user $user | ForEach-Object { $privateChannels = Get-TeamChannel -GroupId $_.GroupId | Where-Object { $_.MembershipType -eq "Private" } if ($privateChannels.Count -eq 0) { Write-Host "$((Get-Team -GroupId $_.GroupId).DisplayName) $($_.GroupId)" $currentRole = (Get-TeamUser -GroupId $_.GroupId | Where-Object { $_.User -eq "$user" }).Role if ($currentRole -eq "owner") { Write-Host "Owner => Member => Owner" # This removes the Owner role... Remove-TeamUser -GroupId $_.GroupId -User $user -Role "Owner" # ... which I then add back Add-TeamUser -GroupId $_.GroupId -User $user -Role "Owner" } if ($currentRole -eq "member") { Write-Host "Member => Owner => Member" # Add as Owner... Add-TeamUser -GroupId $_.GroupId -User $user -Role "Owner" # ... then remove that role Remove-TeamUser -GroupId $_.GroupId -User $user -Role "Owner" } } } |
More later…