PowerShell Queries for Microsoft Teams Administrator

TeamsPowerShell

There is a common concerns to all Microsoft Teams administrators w.r.t managing the Team Users, pulling the data using PowerShell queries/scripts. I am adding all the possible PowerShell queries in order to manage Microsoft Teams, Users and especially pulling the data such as List of Teams, Channels, Ownership, Guests and etc.

TeamsPowerShell
In order to access Microsoft Teams PowerShell module, you will need to Install the Module and connect to Microsoft Teams using your tenant admin credentials (Global Admin / Teams Service Admin). It’s quite simple, run the below commands in your Windows PowerShell tool.

#Install Microsoft Teams Module  
Install-Module MicrosoftTeams 
 
# Add MicrosoftTeams Module to the current session  
Import-Module MicrosoftTeams 

# Get the credentials    
$credentials=Get-Credential   
    
# Connect to Microsoft Teams  
Connect-MicrosoftTeams -Credential $credentials  

Note : The scope of this blog is limited to only Teams (Group) management, playing around only Team cmdlets, list of cmdlets are as follows,

Add-TeamUser                         
Get-CsPolicyPackage                  
Get-CsUserPolicyPackage              
Get-CsUserPolicyPackageRecommendation
Get-Team                             
Get-TeamChannel                      
Get-TeamHelp                         
Get-TeamsApp                         
Get-TeamUser                         
Grant-CsUserPolicyPackage            
New-Team                             
New-TeamChannel                      
New-TeamsApp                         
Remove-Team                          
Remove-TeamChannel                   
Remove-TeamsApp                      
Remove-TeamUser                      
Set-Team                             
Set-TeamArchivedState                
Set-TeamChannel                      
Set-TeamsApp    

Below are the list of PowerShell queries which are useful for your day-day Microsoft Teams tasks.

#1 : List of Teams, the specific user is part of as Member/Owner.
This query will pull the list of the Team’s where the specific ‘user@unifycomm.com‘ user is part of as a Owner or Member.

$User = 'user@unifycomm.com'
$Teams = Get-Team
foreach ($Team in $Teams) { Get-TeamUser -GroupId $Team.GroupId | where {$_.User -eq $User} | Select User, Role, @{n='TeamName';e={$Team.DisplayName}} }

#2 : List of Guest Users and respective Team Name.
This query will to pull the details of all the Guest users and their respective Team’s (Group) name in your tenant.

$Teams = Get-Team
foreach ($Team in $Teams) { Get-TeamUser -GroupId $Team.GroupId | where {$_.Role -eq "Guest"} | Select User, Role, @{n='TeamName';e={$Team.DisplayName} } }

#3 : List of Channels for particular Team
This query will pull details of all the Teams (Group) in your tenant along with it’s respective channels.

$TeamName = Get-Team -DisplayName "Team-Name"
Get-TeamChannel -GroupId $TeamName.GroupId | Select DispLayName, Description

#4 : List of Teams and it’s respective Channels
This query will pull the details of all the Teams (Group) and it’s respective channels in your tenant.

$Teams = Get-Team
$List = foreach ($Team in $Teams) { Get-TeamChannel -GroupId $Team.GroupId | Select-Object @{n='TeamName';e={$Team.DisplayName} }, @{n='ChannelName';e={$_.DisplayName} }, Description  }
$ChannelCount = ($List.DisplayName).Count

Write-Host Total Teams :: $Teams.count -ForegroundColor Green
Write-Host Total Channels :: $ChannelCount -ForegroundColor Green

$List

#5 : Create a common channel in all Teams across tenant (example : Compliance Training Channel)
This query will create a common named/purpose channel in all Teams (Group) in your tenant, basically there will be demand for this in your organization for common topic discussions using common named channel created in all the Teams.

$Teams = Get-Team
foreach ($Team in $Teams) { New-TeamChannel -GroupId $Team.GroupId -DisplayName "Compliance Training" -Description "This channel is to get the regular updates on Compliance trainings" }

#6 : Remove a common channel or specific named channel from all Teams in your tenant (Example : Compliance Training channel)
This query will remove the channel named “Compliance Training” from all the Teams (Group) in your tenant. Basically there will be demand form compliance team, where the some specific naming conventions Channels must not be exists in any of Teams.

$Teams = Get-Team
foreach ($Team in $Teams) { Remove-TeamChannel -GroupId $Team.GroupId -DisplayName "Compliance Training" }

#7 : Add specific Team user to all the Teams across tenant
This query will add specific Team user <ComplianceAdmin@unifycomm.com> to all available Teams (Group) in your tenant and make this account as Owner

$Teams = Get-Team
foreach ($Team in $Teams) { Add-TeamUser -GroupId $Team.GroupId -User admin@unifycomm.com -Role Owner }

#8 : Remove specific Team user from all the Teams across tenant
This query will remove specific Team user from all the available Teams (Group) in your tenant.
Note : If this particular Team user is part of any Teams which have single owner, the query fail/skip and proceed further to execute to rest of the Teams which have multiple Owners.

$Teams = Get-Team
foreach ($Team in $Teams) { Remove-TeamUser -GroupId $Team.GroupId -User admin@unifycomm.com -Role Owner }

#9 : Top 10 Team User with highest Team ownership.
This query will list the top 10 Team users who are with highest number of Team(Group) ownership.

$Teams = Get-Team
$Top10Owners = foreach ($Team in $Teams) { Get-TeamUser -GroupId $Team.GroupId | where {$_.Role -eq "Owner"} } 

($Top10Owners | Group-Object User -NoElement) | Format-Table -AutoSize | Select-Object -First 10

#10 : Top 10 Team User with highest Team membership.
This query will list the top 10 Team users who are with highest number of Team(Group) membership.

$Teams = Get-Team
$Top10Members = foreach ($Team in $Teams) { Get-TeamUser -GroupId $Team.GroupId | where {$_.Role -eq "Member"} } 

($Top10Members | Group-Object User -NoElement) | Format-Table -AutoSize | Select-Object -First 10

#11 :Top 10 Guest Users with highest Team guest’ship.
This query will list the top 10 Guest users who are with highest number of Team (Group) guest’ship.

$Teams = Get-Team
$Top10Guests = foreach ($Team in $Teams) { Get-TeamUser -GroupId $Team.GroupId | where {$_.Role -eq "Guest"} } 

($Top10Guests | Group-Object User -NoElement) | Format-Table -AutoSize | Select-Object -First 10

You can also use these queries in your script, if you are planning one.
Use Export-Csv cmdlet to exports the output for any above queries into CSV file.


If there are any more scenarios comes in your mind, please do comment, will get those added here.

Leave a Reply

Your email address will not be published. Required fields are marked *