Managing Microsoft Teams can be challenging, especially when tracking the creation and configuration of private channels across multiple teams. As Teams administrators, it’s essential to have visibility into newly created private channels to ensure compliance, security, and effective collaboration.
This article presents a PowerShell script using Microsoft Graph that allows you to list all recently created private channels across your Microsoft Teams environment. The script provides details such as the creation date of the channel, the team name, the channel name, the count of owners, and the total number of members in each channel.
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Team.ReadBasic.All" "Channel.ReadBasic.All" "GroupMember.Read.All"
# Define the date range for recently created channels (e.g. last 30 days)
$startDate = (Get-Date).AddDays(-30).ToString("yyyy-MM-ddTHH:mm:ssZ")
# Get all teams
$teams = Get-MgTeam -All
# Initialize an array to store the results
$results = @()
# Iterate through each team to find private channels
foreach ($team in $teams) {
$channels = Get-MgTeamChannel -TeamId $team.Id -Filter "membershipType eq 'private' and createdDateTime ge $startDate"
foreach ($channel in $channels) {
# Get the list of members in the channel
$members = Get-MgTeamChannelMember -TeamId $team.Id -ChannelId $channel.Id -All
# Count the number of owners
$ownerCount = ($members | Where-Object { $_.Roles -contains "owner" }).Count
# Count the total number of members
$totalMembers = $members.Count
# Store the result in the array
$results += [PSCustomObject]@{
'Channel Created Time' = $channel.CreatedDateTime
'Team Name' = $team.DisplayName
'Channel Name' = $channel.DisplayName
'Owner Count' = $ownerCount
'Total Members' = $totalMembers
}
}
}
# Output the results in a table format
$results | Format-Table -AutoSize
This script efficiently identifies and lists recently created private channels in Microsoft Teams. Here’s how it works:
You can enhance this script by adding the following features:
$results | Export-Csv -Path "RecentlyCreatedPrivateChannels.csv" -NoTypeInformation
Cause: If you encounter permission errors, ensure that the account running the script has the necessary permissions in Azure AD to access Microsoft Teams data. You may need to grant additional permissions in the Azure portal.
Solution: Make sure your account is properly scoped for Team.ReadBasic.All, Channel.ReadBasic.All, and GroupMember.Read.All permissions.
Cause: Microsoft Graph API may throttle requests if the script is run against a large number of teams.
Solution: To avoid throttling, consider adding a delay (Start-Sleep) between API calls or handling throttling responses by retrying the request after a pause.
Cause: If the script returns no results, verify that there are indeed private channels created within the specified date range. Additionally, ensure that the script is run with the correct scopes and that private channels exist in the tenant.
Solution: Adjust the $startDate variable or check the Microsoft Teams environment to confirm the presence of recently created private channels.
This PowerShell script provides a powerful tool for administrators to monitor the creation of private channels in Microsoft Teams. By keeping track of newly created private channels, administrators can ensure that Teams are being used appropriately and that private channels are managed according to organizational policies. The script can be further customized to fit specific needs, making it a versatile solution for Microsoft Teams administration.
Feel free to implement and modify this script to suit your requirements and ensure your Microsoft Teams environment is well-managed and secure.
© m365corner.com. All Rights Reserved. Design by HTML Codex