Microsoft Teams – How to Generate an Easy Report of all Users & Teams

I’ve recently had a requirement to allow us to generate a report of all the users in our Microsoft Teams teams because as often seems to be the case it’s all grown a little organically and now we need to get it under control. The first step to any process like this though is to find out the current status of your systems and for this you need to be able to easily report on it.

This article will cover a brief script using PowerShell that will export all Teams and Users in those teams along with relevant details into a nice CSV which you can easily process to produce reports.

Example console output

The Script

The script below will connect to Teams using provided credentials and retrieve a list of all Teams and Users, before generating a CSV file. Be sure to replace the value of the $exportLocation variable which a valid location.

<#
|------------------------------------------------------------ Microsoft Teams Report ------------------
| Written by Shaun Wilkinson                                                                                                                        
| Generates a report showing all teams and users                                                                                                    
| Requirements: Access to all teams and Team PS module - https://www.powershellgallery.com/packages/MicrosoftTeams                                  
|------------------------------------------------------------------------------------------------------
#>


$exportLocation = "C:\users\Shaun.Wilkinson\Desktop\export.csv"


Connect-MicrosoftTeams

# Get all of the team Groups IDs
$AllTeamsInOrg = (Get-Team).GroupID
$TeamReport = @()

# Will hold a basic count of user types and teams
$unavailableTeamCount = 0
$knownOwnersCount = 0
$knownMemberCount = 0
$knownGuestCount = 0

# Loop through all Group IDs
$currentIndex = 1
ForEach($Team in $AllTeamsInOrg) {
    # Show a nice progress bar as this can take a while
    Write-Progress -Id 0 -Activity "Building report from Microsoft Teams" -Status "$currentIndex of $($allTeamsInOrg.Count)" -PercentComplete (($currentIndex / $allTeamsInOrg.Count) * 100)

    # Get properties of the team
    $team = Get-Team -GroupId $Team

    # Attempt to get team users, throw error message if no access
    try {
        # Get team members
        $users = Get-TeamUser -GroupId $team.groupID

        # foreach user create a line in the report
        ForEach($user in $users) {
            # Maintain a count of user types
            switch($user.Role) {
                "owner" { $knownOwnersCount++ }
                "member" { $knownMemberCount++ }
                "guest" { $knownGuestCount++ }
            }

            # Create an object to hold all values
            $teamReportObject = New-Object PSObject -Property @{
                TeamName = $team.DisplayName
                Description = $team.Description
                Archived = $team.Archived
                Visibility = $team.Visibility
                User = $user.Name
                Email = $user.User
                Role = $user.Role
            }

            # Add to the report
            $TeamReport += $teamReportObject
        }
    } catch [Microsoft.TeamsCmdlets.PowerShell.Custom.ErrorHandling.ApiException] {
        Write-Host -ForegroundColor Yellow "No access to $($team.DisplayName) team, cannot generate report"
        $unavailableTeamCount++
    }

    
    $currentIndex++
}
Write-Progress -Id 0 -Activity " " -Status " " -Completed

# Disconnect from the teams service
Disconnect-MicrosoftTeams

# Provide some nice output
Write-Host -ForegroundColor Green "============================================================"
Write-Host -ForegroundColor Green "                Microsoft Teams User Report                 "
Write-Host -ForegroundColor Green ""
Write-Host -ForegroundColor Green "  Count of All Teams - $($AllTeamsInOrg.Count)                "
Write-Host -ForegroundColor Green "  Count of Inaccesible Teams - $($unavailableTeamCount)         "
Write-Host -ForegroundColor Green ""
Write-Host -ForegroundColor Green "  Count of Known Users - $($AllTeamsInOrg.Count)                "
Write-Host -ForegroundColor Green "  Count of Known Owners - $($knownOwnersCount)                "
Write-Host -ForegroundColor Green "  Count of Known Members - $($knownMemberCount)                "
Write-Host -ForegroundColor Green "  Count of Known Guests - $($knownGuestCount)                "


$TeamReport | Export-CSV $exportLocation -NoTypeInformation
Write-Host -ForegroundColor Green "Exported report to $($exportLocation)"

Raw Pastebin Link

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.