Listing Recently Created Private Channels in Microsoft Teams

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.


The Script

# 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

How the Script Works

This script efficiently identifies and lists recently created private channels in Microsoft Teams. Here’s how it works:

  1. Connecting to Microsoft Graph: The script starts by establishing a connection to Microsoft Graph using the Connect-MgGraph cmdlet. The required scopes (Team.ReadBasic.All, Channel.ReadBasic.All, GroupMember.Read.All) ensure the script has the necessary permissions to read team, channel, and member information.
  2. Defining the Date Range: The script defines a date range to identify recently created channels. In this example, channels created in the last 30 days are considered. The date range can be easily adjusted by modifying the $startDate variable.
  3. Fetching Teams and Channels: The script retrieves all teams using Get-MgTeam -All and iterates through each team to fetch private channels created within the specified date range. The Get-MgTeamChannel cmdlet is used with a filter to retrieve only private channels.
  4. Counting Owners and Members: For each private channel, the script fetches the list of members using Get-MgTeamChannelMember. It then counts the number of owners (members with the "owner" role) and the total number of members.
  5. Displaying the Results: The results are stored in a custom object array and displayed in a tabular format using Format-Table. The output includes the channel creation time, team name, channel name, owner count, and total members.

Further Enhancements

You can enhance this script by adding the following features:

  • Export to CSV: Modify the script to export the results to a CSV file for further analysis or record-keeping.
    $results | Export-Csv -Path "RecentlyCreatedPrivateChannels.csv" -NoTypeInformation
  • Custom Date Range Input: Allow administrators to input a custom date range to filter channels created within a specific timeframe.
  • Filtering by Team Name: Add functionality to filter results by specific team names or team IDs.
  • Email Notification: Automatically send the results to administrators via email using the Send-MailMessage cmdlet.

Possible Errors & Solutions

Permission Issues

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.

Throttling

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.

Empty Results

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.


Conclusion

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.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex