Skip to content

Automating Microsoft Teams Channel Membership Audit with PowerShell

Automating Microsoft Teams Channel Membership Audit

Section titled “Automating Microsoft Teams Channel Membership Audit”

Microsoft Teams has become a key tool for collaboration within organizations, and as its usage grows, managing and auditing channel memberships can become a complex task. The PowerShell script discussed in this article automates the process of retrieving all channels and their respective members across every team in a Microsoft 365 tenant.


This script performs the following actions:

FunctionDescriptionBusiness Value
Teams ConnectionEstablishes administrative connectionEnables data access
Data RetrievalCollects all teams and channelsComprehensive coverage
Membership AnalysisExtracts member informationDetailed audit data
Data OrganizationStructures membership dataReporting ready
FeatureCapabilityImpact
Complete CoverageScans all teams and channelsNo gaps in audit
Enhanced DataAdds context to member recordsRich reporting
Export ReadyStructured for CSV outputEasy analysis
ScalableHandles large environmentsEnterprise ready

Before running the script, ensure you have:

RequirementDescriptionPurpose
Microsoft Teams PowerShell ModuleInstalled and configuredAPI access
Administrative PermissionsTeam and channel access rightsData retrieval
PowerShell EnvironmentWindows PowerShell 5.1+Script execution

The script starts by connecting to Microsoft Teams with administrative permissions using the Connect-MicrosoftTeams cmdlet.

Terminal window
Connect-MicrosoftTeams

This establishes the necessary authentication, allowing the script to interact with the Teams environment and retrieve team and channel information.

The script uses the Get-Team cmdlet to retrieve all teams within the tenant. It loops through each team and retrieves the channels within that team using the Get-TeamChannel cmdlet.

Terminal window
$teams = Get-Team
$Channels = @()
$Users = @()
Foreach ($team in $teams) {
Write-Verbose "Retrieving $($team.DisplayName) Channels & Members..."
$Channels = Get-TeamChannel -GroupId $team.GroupId
}

This part of the script builds the list of channels for each team, ensuring that all relevant data is gathered.

For each channel, the script retrieves the list of members using the Get-TeamChannelUser cmdlet. It then enhances the member data by adding additional properties, such as the channel name, team name, channel ID, and team ID.

Terminal window
foreach ($channel in $Channels){
$members = Get-TeamChannelUser -GroupId $($team.GroupId) -DisplayName $($channel.DisplayName)
foreach ($member in $members){
$member | Add-Member -MemberType NoteProperty -Name "Channel Name" -Value $($channel.DisplayName)
$member | Add-Member -MemberType NoteProperty -Name "ChannelId" -Value $($channel.Id)
$member | Add-Member -MemberType NoteProperty -Name "Team Name" -Value $($team.DisplayName)
$member | Add-Member -MemberType NoteProperty -Name "TeamId" -Value $($team.GroupId)
$Users += $member
}
}

By using Add-Member, the script appends details about the channel and team to each member record. This makes the data more comprehensive and useful for reporting or further manipulation.

The script stores all the user membership data in the $Users array. This data can then be output to the console or used for further processing, such as exporting to a CSV file for audits or reporting.

Terminal window
$Users

This script can be used to generate a comprehensive audit of all channel memberships across Microsoft Teams. By running the script periodically, administrators can ensure that team memberships remain compliant with internal policies.

The $Users array can be exported to a CSV file for further analysis:

Terminal window
$Users | Export-Csv -Path "C:\path\to\output\teams_channel_members.csv" -NoTypeInformation

This provides a complete snapshot of which users belong to which channels in which teams.

ScenarioApplicationValue
Security AuditsReview access permissionsIdentify security risks
Compliance ReportingGenerate membership reportsMeet regulatory requirements
Access ReviewsValidate user permissionsEnsure proper access
OffboardingCheck departing user accessClean up permissions

Best PracticeDescriptionImplementation
Administrative PrivilegesEnsure proper access rightsPrevent permission errors
Test EnvironmentVerify script functionalityReduce deployment risks
Regular SchedulingAutomate periodic auditsMaintain ongoing compliance
Data BackupPreserve audit resultsCreate historical records

As teams grow and evolve, managing channel memberships manually becomes increasingly difficult.

ChallengeManual ProcessAutomated Solution
Time RequirementsDays of manual workHours of automated processing
AccuracyHuman error proneConsistent results
CoverageMay miss teams/channelsComplete enumeration
ReportingManual compilationAutomated generation
BenefitDescriptionImpact
Security PostureIdentify unauthorized accessReduce security risks
Compliance MaintenanceRegular access reviewsMeet regulatory standards
GovernanceCentralized visibilityImprove control
Audit TrailHistorical membership dataSupport investigations

Each member record includes the following enhanced properties:

PropertyDescriptionSource
Channel NameDisplay name of channelGet-TeamChannel
ChannelIdUnique channel identifierGet-TeamChannel
Team NameDisplay name of teamGet-Team
TeamIdUnique team identifierGet-Team
User PropertiesStandard user informationGet-TeamChannelUser

Key Takeaway: This PowerShell script offers a streamlined way to audit Microsoft Teams channel memberships, gathering and organizing membership information across all teams and channels within a tenant. By automating the retrieval of this data, IT administrators can save time and ensure that they maintain an accurate and up-to-date record of team and channel memberships.

Whether you’re conducting a security audit, generating reports, or simply ensuring that team memberships are accurate, this script provides a powerful solution for managing Teams at scale. By integrating it with reporting tools or scheduling it to run periodically, you can maintain tight control over your Teams environment and ensure compliance with your organization’s policies.