Automate adding tags to team members

Published on Wednesday, October 19, 2022



I see that there is no way to ease the job of adding a tag to multiple users at the same time without having to look for the user in the edit tag window, so if you have a large list of team members and you want to add the tag to a lot of them or all, it is a little tedious, so exploring the API as these other posts Getting some extra information about Teams federated users using PowerShell and Mark Teams notifications as read we can script this calling the chat service API.


Tag name

The tag you want to add to users doesn't need to be already created, this also means that, for example you make a typo specifying the tag name, it will be created, so make sure you type the right tag name.

The team's object id

Every entity in Teams is represented by an MRI, in this case we would need to grab the object id part of the mri from the URL.


  1. Open Teams using the Web browser
  2. Click on the three "..." and Manage team
  3. Look at the address bar, for example
  4. Copy everything between ":" and "@"

The UserIds

You can grab the user ids, using the official Microsoft Teams PowerShell module, using the Get-TeamUser cmdlet, take note of the UserId you need (corresponding to the user you need to assign a tag to).

The CSV file

The format of the csv file is this


for example


The script

function Add-TagsToUsers{

  #CSV file containing the team objectId, user id and tag name
  $csv = Import-CSV -Delimiter ";" -Path $userAndTags

  ## Let's load the great AADInternals module
  Import-Module AADInternals
  #Let's request a chat service token
  $token = Get-AADIntAccessToken -Resource -ClientId "1fec8e78-bce4-4aaf-ab1b-5451cc387264"

  #Some variable definitions - Do not change this

  #For each entry in the csv we will try to add the user to the tag
  foreach($entry in $csv){


      $uri = $urlPart1+$objectId+$urlPart3
      Write-Verbose $uri
      $body = "{`"tagNames`":[`"$tag`"],`"memberIds`":[`"8:orgid:$userId`"]}"  
      Write-Verbose $body  
      Write-Verbose $path
      $Result=Invoke-WebRequest -UseBasicParsing -Uri $uri `
      -Method "POST" `
      -Headers @{
        "accept-encoding"="gzip, deflate, br"
        "authorization"="Bearer $token"
        "sec-ch-ua"="`"Chromium`";v=`"106`", `"Microsoft Edge`";v=`"106`", `"Not;A=Brand`";v=`"99`""
      } `
      -ContentType "application/json" `
      -Body $body `



The script uses the great AADInternals module to ease the token retrieval and then mimics what the Teams client does when adding tags to users. Once run, you should have all the users you specified in the csv file with the specified tag in the specified team.

I invite everyone that wants to contribute to this script to do so here in this GitHub repo.

comments powered by Disqus