Controlling Naming in Microsoft Teams

How to prevent users from renaming the Teams

@MattBrowne

3 minute read

Microsft Teams is one of those products that seems to explode when it’s rolled out. Once it goes viral within the organisation, as it seems to in most, then the number of Teams seems to grow rapidly. This can get out of control and messy unless you have a naming convention. It’s possible to restrict who can create Teams. However it’s difficult to stop people from renaming the Team once it’s created.

In this post, we are going to look at controlling the name of the Teams via a script and a ‘master’ file for the naming.

The Code

Before we do anything lets take a look at the code…….

<#
.SYNOPSIS
  Controls the naming of Teams
.DESCRIPTION
  Script to monitor the naming of Teams and correcting them if they are changed.
.NOTES
  Version:        1.0
  Author:         Matt Browne
  Creation Date:  Jan/2020
  Purpose/Change: Initial script development
  Requires: The PowerShell module for Teams (https://docs.microsoft.com/en-us/microsoftteams/teams-powershell-overview)
  To Dos: Needs some logging
#>

Import-Module MicrosoftTeams

$masterFile = "C:\Test\Teams.csv"

Connect-MicrosoftTeams -Credential $(Get-Credential)

if (Test-Path -Path $masterFile -ErrorAction SilentlyContinue) {
    $masterTeamList = Import-Csv -Path $masterFile
    $teams = Get-Team
    foreach ($team in $teams) {
        if ($masterTeamList.GroupId -notcontains $team.GroupId) {
            Write-Output "New Team added to the master list : $($team.DisplayName)"
            $team | Select-Object GroupId, DisplayName, Description | Export-Csv -Path $masterFile -Append
        } else {
            $teamName = $team.displayname
            $officalTeamName = $masterTeamList | Where-Object { $_.GroupId -eq $team.GroupId } | Select-Object -ExpandProperty DisplayName
            if ($teamName -ne $officalTeamName) {
                Set-Team -GroupId $team.GroupId -DisplayName $officalTeamName
                Write-Output "Team -> $officalTeamName, corrected from -> $teamName"
            }
        }
    }
} else {
    Write-Output "Initialising master file.  This will not rename any Teams........"
    $teams = Get-Team
    $teams | Select-Object GroupId, DisplayName, Description | Export-Csv -Path $masterFile
}

A bit about the code

The script relies on the PowerShell Teams Module. This allows us to list all the Teams and save them to a ‘Master’ file.

If you want to change the location of the Master file, change the following line…..

$masterFile = "C:\Test\Teams.csv"

This CSV file holds a list of the Teams that currently exist. The important part is it holds the unique identifier for the Team. This means that whatever the name changes to, we can always identify it.

From this, the script will run through all the Teams and change the name to what is in the master file (if it is different).

Note - If you have a large number of Teams the script could take a while to run.

Running the script

If you want to run the script as a test, ie without it changing anything, then comment out the following line.

Set-Team -GroupId $team.GroupId -DisplayName $officalTeamName

This means that the script will run and have the normal output but won’t make any changes.

When you run the script it will ask for credentials. This needs to be a Teams admin. If you are going to run this on a schedule then it’s worth having a sensible name for the account. This is because it will appear in the Team as the user that has changed the name back.

The results

Once you run the script it will look through all the Teams. It then renames anything that doesn’t match what is in the Master file.

The result of will be a message in the Teams ‘General’ Channel showing that the name has changed back.

Teams Name Correction XXX

Test Image

If the name of the Team needs to change for some reason then it needs to be edited in the CSV file. The script will then make the changes the next time it is run.

Summary

Run the script on a regular basis and you will have a consistent set of Team names. So when the boss asks you “How to stop people renaming Teams” you have an answer.