Microsoft Teams : Governance – Fetching single owner Teams through PowerShell

Life won’t sparkle unless you do.
Hello Everyone,
Hope you all are doing well.
In this article we are going to discuss how to fetch single owner Teams and generate csv file through PowerShell.
So without getting late, let’s get started.
Background
In our organization, one of our project requirement is to fetch the details having single owner in Microsoft Teams and generate record for the same.
Details
- As on fetching the details, we wrote a script and ran it through PowerShell.
- And also we generate a csv file with having single owner in Teams through PowerShell.
Prerequisites
- Install PowerShell ISE / PowerShell / Visual Studio Code
- Install Module Microsoft Teams
# Install Microsoft Teams
Install-Module Microsoft Teams
Detailed Steps – PowerShell script
- Connect to the Microsoft Teams.
# Connect Microsoft Teams of our Tenant
Connect-MicrosoftTeams
- Define a new object / variable to gather the output.
# Define a new object to gather output
$teamsCollection= @()
- Get all Teams in our Tenant.
# Get all the Teams
Write-Verbose "Getting Team Names and Details"
$teams = Get-Team
- Display the Teams count in our Tenant.
# Get the Teams count
Write-Host "Teams Count is $($teams.count)"
- Once we have teams collection loop through each team.
- And then get Owners count.
# Get Owners count
$TeamUsers = Get-TeamUser -GroupId $_.GroupID
$TeamOwnerCount = ($TeamUsers | Where-Object {$_.Role -like "owner"}).count
- Now get each team details in to a variable / object.
# Put all details into an object
$output = New-Object -TypeName PSobject
$output | add-member NoteProperty "TeamName" -value $_.DisplayName
$output | add-member NoteProperty "Description" -value $_.Description
$output | add-member NoteProperty "Visibility" -value $_.Visibility
$output | Add-Member NoteProperty "OwnerCount" -Value $TeamOwnerCount
$output | add-member NoteProperty "GroupId" -value $_.GroupId
$teamsCollection += $output
- Add each team details to the teams collection.
# Set the output object
$teamsCollection += $output
- Fetch the Teams having single owner with details from the output collection.
$teamsCollection | Where-Object {$_.Ownercount -eq 1} | Select-Object GroupId, TeamName, Visibility
Note: Here we can filter the owner count as per our requirement like 0/1/2/…..
- Create a csv file, give the path and content headings.
# Create CSV file and add headings in CSV file
Add-Content -Path E:\PowerShellScripts\singleowner.csv -Value '"GroupID","TeamName","Visibility"'
- Use the collection to generate a csv file.
# Generate csv file
$teamsCollection | Where-Object {$_.Ownercount -eq 1} | Select-Object GroupId, TeamName, Visibility | Export-Csv -Path E:\PowerShellScripts\singleowner.csv
- Finally we fetch the details having single owner Teams and generated csv file for the same.
Complete Script
<#
========================================================================================================
Name: Fetching the Teams/Groups having single owner in Microsoft Teams through PowerShell
Description: This script searches for single owner Teams/Groups in an Organization through PowerShell
Version: 1.0
========================================================================================================
#>
# Install Microsoft Teams
Install-Module Microsoft Teams
# Import Microsoft Teams
Import-Module Microsot Teams
# Connect Microsoft Teams
Connect-MicrosoftTeams
# Define a new object to gather output
$teamsCollection= @()
# Get all the Teams
Write-Verbose "Getting Team Names and Details"
$teams = Get-Team
# Get the Teams count
Write-Host "Teams Count is $($teams.count)"
$teams | ForEach-Object {
Write-host "Getting details for Team $($_.DisplayName)"
# Get Owners
$TeamUsers = Get-TeamUser -GroupId $_.GroupID
$TeamOwnerCount = ($TeamUsers | Where-Object {$_.Role -like "owner"}).count
# Put all details into an object
$output = New-Object -TypeName PSobject
$output | add-member NoteProperty "TeamName" -value $_.DisplayName
$output | add-member NoteProperty "Description" -value $_.Description
$output | add-member NoteProperty "Visibility" -value $_.Visibility
$output | Add-Member NoteProperty "OwnerCount" -Value $TeamOwnerCount
$output | add-member NoteProperty "GroupId" -value $_.GroupId
$teamsCollection += $output
}
# Output collection
$teamsCollection
$teamsCollection | Where-Object {$_.Ownercount -eq 1} | Select-Object GroupId, TeamName, Visibility
# Create CSV file and add headings in CSV file
Add-Content -Path E:\PowerShellScripts\singleowner.csv -Value '"GroupID","TeamName","Visibility"'
# Generate CSV file
$teamsCollection | Where-Object {$_.Ownercount -eq 1} | Select-Object GroupId, TeamName, Visibility | Export-Csv -Path E:\PowerShellScripts\singleowner.csv
.
Hope this article will help us to fetch the details of Microsoft Teams having single owner through PowerShell.
Also get my article updates on my social media handles.
LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/
Twitter – https://twitter.com/KhasimShaik2009
Facebook – https://www.facebook.com/profile.php?id=100078255554660
Thank you for your support, will catch up with new article soon.
Keep learning and keep smiling.
Thanks.