August 3, 2020

Microsoft Teams: Export Team Members to Excel

In this post I explain how to export the list of a Microsoft Teams Team members to an Excel file including the email of each user, using a PowerShell script from an AutoHotkey script.
This is included in the Teams Shortcuts PowerTool.

PowerTool Feature Usage


From Teams Shortcuts menu from the System Tray icon, select "Export Team Member"
or from Teamsy Launcher use the Keyword "t2xl".

As input it requires a link to a Team (containing the groupId element).  
See screenshot below how to get a team link: Click on the ... menu and select Get link to team

The Excel output is a nice formatted excel file with a Table inside.
It includes Name, email, FirstName, LastName and Role in the Team (Member or Owner)

You need to save the file manually.

You can then easily copy/paste the email addresses for example to Outlook.

Implementation in AutoHotkey

The code is available in my GitHub repository ahk/Lib/Teams.ahk -> Teams_users2excel

It can be decomposed in 2 parts:

  • export users to a CSV file using basic PowerShell Get-TeamUser command
  • Loop on the CSV to extract Firstname/Lastname and email + reformat in an Excel Table

Export users to CSV file

The core PowerShell command is:
Get-TeamUser -GroupId %sGroupId% | Export-Csv -Path %CsvFile% -NoTypeInformation

This main command is wrapped in a .ps1 file that also includes the Connect-MicrosoftTeams command.

Prerequisite for the PowerShell to run is that Microsoft Teams PowerShell has been set up.

Format into an Excel Table and Extract email

This part uses Excel ComObj interface from AutoHotkey.

Extract Email using Active Directory query

This is done by the function in Lib/People/People_ADGetUserField that will run an Active Directory query.
mail := People_ADGetUserField("mailNickname=" . sUid, "mail") ; mailNickname - office uid

The AD Domain shall be provided once for the PowerShell scripts to run.

See also

Add list of users to a Microsoft Teams Team e.g. from Outlook, Excel using the PowerTools

No comments:

Post a Comment