Automating the Identification of Inactive Microsoft Teams and Channels Using PowerShell

Managing Microsoft Teams environments efficiently requires regular monitoring of team activity to identify inactive teams and channels that may no longer be needed. This PowerShell script automates the process of checking whether any Microsoft Teams channels have any messages, helping administrators to clean up unused resources. Below, we break down the script’s functionality and explain each step in detail.

Script Overview

The script imports data from a CSV file, iterates through each site listed, checks if the site is connected to Microsoft Teams, and then determines if any messages exist in the team’s channels. Finally, it exports the results to a CSV file.

Prerequisites

Before running the script, ensure you have the following prerequisites:

  • The SharePoint PnP PowerShell module installed and you are using PowerShell version 7 or above.
  • Proper permissions to connect to the Microsoft Teams sites.
  • A CSV file named SitesList.csv with columns: URL, sitename, and Teams. You can download the sites list from SharePoint admin centre. You need to change the first column header as sitename and remove the quotations.

Step-by-Step Breakdown

1. Importing Data from CSV

				
					$data = Import-Csv "C:\scripts\FindEmptyTeams\SitesList.csv"
$TeamsData=@()
				
			
This line imports data from SitesList.csv and initialises an empty array $TeamsData to store the results

2. Iterating Through Each Row:

				
					foreach ($row in $data) {
				
			

The script loops through each row of the CSV file.

3. Checking If the Site is Connected to MS Teams:

				
					if($row.Teams -eq "TRUE"){
				
			

This condition checks if the site is connected to Microsoft Teams.

4. Connecting to the Specific Site:

				
					Connect-PnPOnline -Url $row.URL -Interactive
write host "connected to" $row.URL
				
			

The script connects to the specified site URL using Connect-PnPOnline and confirms the connection.

5. Retrieving Channels:

				
					$Channels = Get-PnPTeamsChannel -Team $row.sitename | select DisplayName
				
			

This line retrieves all channels for the given team and collect the display name of each channel.

6. Checking for Messages in Each Channel:

				
					foreach ($channel in $Channels) {
    $Message = Get-PnPTeamsChannelMessage -Team $row.sitename -Channel $channel.DisplayName | Where-Object -FilterScript {$_.Body.Content -ne '<systemEventMessage/>'}
    if ($Message -ne $null) {
        break
    } else {
        $Message = $null
    }
}
				
			

The script iterates through each channel, retrieves messages, and checks if they are any messages exists. If any user messages are found, it exits the loop.

7. Storing Results:

				
					if ($Message -eq $null) {
    $TeamsData += New-Object PSObject -Property @{
        SiteURL = $row.URL
        TeamName = $row.sitename
        TeamChat = "No"
    }
} else {
    $TeamsData += New-Object PSObject -Property @{
        SiteURL = $row.URL
        TeamName = $row.sitename
        TeamChat = "Yes"
    }
}
				
			

Based on whether messages were found, the script adds an object to $TeamsData indicating if the team chat is active or not.

8. Exporting Results to CSV:

				
					$TeamsData | Export-Csv "C:\scripts\FindEmptyTeams\TeamsOutputList.csv" -NoTypeInformation
				
			

Finally, the script exports the collected data to TeamsOutputList.csv.

Full Script:

				
					#Import data from csv
$data = Import - Csv "C:\scripts\FindEmptyTeams\SitesList.csv"
$TeamsData = @()

#Itrating through each row
foreach($row in $data) {
  if ($row.Teams - eq "TRUE") {
    #Connecting to that specific site
    Connect - PnPOnline - Url $row.URL - Interactive
            write host "connected to" $row.URL
    $Channels = Get - PnPTeamsChannel - Team $row.sitename | select DisplayName
    foreach($channel in $Channels){
      $Message = Get - PnPTeamsChannelMessage - Team $row.sitename - Channel $channel.DisplayName | Where - Object - FilterScript { $_.Body.Content - ne '<systemEventMessage/>' }
      if ($Message - ne $null) {
        break
      }else {
        $Message = $null
      }
    }
    if ($Message - eq $null) {
      $TeamsData += New - Object PSObject - Property @{
        SiteURL = $row.URL
              TeamName=$row.sitename
          TeamChat="No"
      }
    }else {
      $TeamsData += New - Object PSObject - Property @{
        SiteURL = $row.URL
                TeamName=$row.sitename
            TeamChat="Yes"
      }
    }
  }
}

$TeamsData | Export - Csv "C:\scripts\FindEmptyTeams\TeamsOutputList.csv"
				
			

Conclusion

This PowerShell script efficiently automates the process of identifying inactive Microsoft Teams  channels, allowing administrators to manage their Teams environments more effectively. By running this script regularly, you can ensure that only active and necessary teams and channels are maintained, contributing to a more organised and clutter-free Teams workspace.

How KeaPoint Can Help

KeaPoint can further enhance your Microsoft Teams management by providing comprehensive tools and insights to organise your teams and channels efficiently. With KeaPoint, you can easily identify inactive teams, streamline communication, and ensure that your Microsoft Teams environment remains productive and clutter-free. By integrating KeaPoint with your existing workflows, you can automate routine tasks, gain deeper insights into team activity, and maintain a well-organized digital workspace. Let KeaPoint help you take control of your Teams environment and maximise your organisational efficiency.

More Blogs

Contact us

To begin your digital transformation, get in touch.

We’re pleased to address any inquiries you might have and assist you in selecting the service that best suits your requirements.

Your benefits:
Speak To Us