Managing Microsoft Teams effectively involves identifying team owners, an essential aspect of team governance. PowerShell provides a powerful way to automate this process. In this guide, we'll show you how to export Microsoft Teams owners to a CSV file using PowerShell.
Get the Team Owners Using PowerShell
Prerequisites
Before you begin, make sure you have the following in place:
- PowerShell Modules: You'll need to install and import the "Teams" and "AzureAD" modules.
- Permissions: Ensure you have the necessary permissions within your Microsoft 365 environment to retrieve team and user information.
Step 1: Install and Import Required Modules
To get started, install and import the necessary PowerShell modules:
Install-Module -Name "Teams" -Force -AllowClobber
Install-Module -Name "AzureAD" -Force -AllowClobber
Import-Module -Name "Teams"
Import-Module -Name "AzureAD"
Step 2: Connect to Your Microsoft 365 Tenant
Establish a connection to your Microsoft 365 tenant using PowerShell:
Connect-MicrosoftTeams
Connect-AzureAD
Step 3: Retrieve Teams and Owners
Run the following script to loop through all teams, retrieve the owners, and export the results to a CSV file:
# Get a list of all teams
$teams = Get-Team
# Create an array to store the results
$results = @()
# Loop through the teams
foreach ($team in $teams) {
$teamDisplayName = $team.DisplayName
$ownerColl = Get-TeamUser -GroupId $team.GroupId -Role Owner
# Loop through the owners
foreach ($owner in $ownerColl) {
$ownerUserId = $owner.UserId
$ownerName = $owner.Name
# Fetch the UPN for the owner
$ownerUser = Get-AzureADUser -ObjectId $owner.UserId
$ownerUPN = $ownerUser.UserPrincipalName
# Create a custom object to store the data
$ownerData = New-Object PSObject -Property @{
"TeamDisplayName" = $teamDisplayName
"UserID" = $ownerUserId
"Name" = $ownerName
"UPN" = $ownerUPN
}
# Add the custom object to the results array
$results += $ownerData
}
}
# Export the results to a CSV file with UTF-8 encoding to handle special characters
$results | Export-Csv -Path "TeamOwners.csv" -NoTypeInformation -Encoding UTF8
Summary
Automating the process of exporting Microsoft Teams owners to a CSV file using PowerShell streamlines team management and ensures a structured approach to governance. With the ability to gather and store this data efficiently, organizations can make the most of their collaboration platform.
Please ensure you have the necessary permissions and follow your organization's policies when using this script.
Frequently Asked Questions
What permissions do I need to run Get-Team in PowerShell?
You need to be a Global Administrator or Teams Service Administrator to retrieve a list of all teams. Team owners can only retrieve the teams they own.
Can I export Teams members as well as owners?
Yes. Change the -Role Owner parameter in Get-TeamUser to -Role Member to export members. You can also remove the -Role parameter entirely to export all users regardless of role.
Why am I getting an access denied error with Connect-MicrosoftTeams?
Ensure your account has the required admin role and that multi-factor authentication is completed. If your organization uses Conditional Access policies, you may need to use a compliant device or approved client app.
How do I export owners for a specific team only?
Replace $teams = Get-Team with $teams = Get-Team -DisplayName "Your Team Name" to filter by team name, or use -GroupId to target a specific team by its Group ID.
Is the AzureAD module still required for this script?
The AzureAD module is used to fetch the UserPrincipalName (UPN). If you only need the owner name and user ID, you can skip the AzureAD module and remove the UPN-related lines from the script.
