How to Export Microsoft Teams Owners to a CSV File Using PowerShell
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
# 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
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