May 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

List all Microsoft Teams using PowerShell

Last week, I have been faced with a serious issue for which I had to act very quickly.

A typical SharePoint search started showing content from different teams, which have been all set as public teams.

A direct result of quickly jumping into using Teams due to the current pandemic and the need for remote work enabling, without enough time to think about Teams Governance or the security aspect of the content.

I am sure, this is the case of many organisations.

The problem was to find all the public teams and quickly lock them down to private teams and private channels.

Doing this via the Teams Admin portal would have taken days, so I had to write a PowerShell Script to first list all the public teams, then switch them to private after internal review.


List all the teams


The following PowerShell script requires the following:

  1. Microsoft Teams module for powershell  available from:
    1. Install the Microsoft Teams PowerShell module via PowerShell Gallery(recommended option).
    2. Install the Microsoft Teams PowerShell module via Manual Download.
  2. Having Teams or Tenant admin rights.

Once these two requirements were in place, time for the action:

The Script


$Result = ""  
$Results = @() 
$Path = "./Teams Report_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv"
Write-Host Exporting all Teams report...
$Count = 0
Get-Team | foreach {
    $TeamName = $_.DisplayName
    Write-Progress -Activity "`n     Processed Teams count: $Count "`n"  Currently Processing: $TeamName"
    $Visibility = $_.Visibility
    $MailNickName = $_.MailNickName
    $Description = $_.Description
    $Archived = $_.Archived
    $GroupId = $_.GroupId
    $ChannelCount = (Get-TeamChannel -GroupId $GroupId).count
    $TeamUser = Get-TeamUser -GroupId $GroupId
    $TeamMemberCount = $TeamUser.Count
    $TeamOwnerCount = ($TeamUser | ? { $_.role -eq "Owner" }).count
    $Result = @{'Teams Name' = $TeamName; 'Team Type' = $Visibility; 'Mail Nick Name' = $MailNickName; 'Description' = $Description; 'Archived Status' = $Archived; 'Channel Count' = $ChannelCount; 'Team Members Count' = $TeamMemberCount; 'Team Owners Count' = $TeamOwnerCount }
    $Results = New-Object psobject -Property $Result
    $Results | select 'Teams Name', 'Team Type', 'Mail Nick Name', 'Description', 'Archived Status', 'Channel Count', 'Team Members Count', 'Team Owners Count' | Export-Csv $Path -NoTypeInformation -Append
Write-Progress -Activity "`n     Processed Teams count: $Count "`n"  Currently Processing: $TeamName" -Completed
if ((Test-Path -Path $Path) -eq "True") {
    Write-Host `nReport available in $Path -ForegroundColor Green