Listing Recently Created Microsoft Teams Shared Channels

Shared channels in Microsoft Teams provide a flexible way for teams to collaborate across different organizations while keeping the conversations and files within a single channel. For Teams administrators, it is crucial to monitor and manage these channels to ensure that they align with organizational policies and security standards.

This article presents a PowerShell script that utilizes Microsoft Graph to list all recently created shared channels across teams. The script provides detailed information such as the creation date of the channel, the team name, the channel name, the number 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 shared 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 shared channels
foreach ($team in $teams) {
    $channels = Get-MgTeamChannel -TeamId $team.Id -Filter "membershipType eq 'shared' 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 lists all recently created shared channels across Microsoft Teams. Here’s how it operates:

  1. Connecting to Microsoft Graph: The script begins 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 that the script has the necessary permissions to read team, channel, and member information.
  2. Defining the Date Range: The script sets a date range to identify recently created shared channels. In this example, channels created within 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 shared channels created within the specified date range. The Get-MgTeamChannel cmdlet is used with a filter to retrieve only shared channels.
  4. Counting Owners and Members: For each shared channel, the script retrieves 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.
  • 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, the account running the script may lack the necessary permissions in Azure AD to access Microsoft Teams data.

Solution: Ensure that the account has the required permissions. You may need to grant additional permissions in the Azure portal.

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, it could mean that no shared channels were created within the specified date range.

Solution: Verify the date range and ensure that the correct scopes are used when running the script.


Conclusion

This PowerShell script provides a practical solution for administrators to monitor the creation of shared channels in Microsoft Teams. By keeping track of newly created shared channels, administrators can ensure that teams collaborate effectively while adhering to organizational policies and security standards. The script can be easily customized and enhanced to meet specific needs, making it a valuable tool in the administration of Microsoft Teams.

This article should serve as a guide to help administrators understand the importance of monitoring shared channels and how to leverage Graph PowerShell for effective Teams management. Feel free to implement and modify the script according to your requirements to keep your Teams environment secure and well-managed.


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