In Microsoft 365, managing Teams is essential for maintaining security and efficiency. While standard cmdlets like Remove-MgTeamMember exist, there are cases where you may need to interact directly with Microsoft Graph API endpoints, such as removing members from a Teams channel. This article explains how to use the Invoke-MgGraphRequest cmdlet to remove channel members, complete with syntax, real-world usage examples, cmdlet tips, possible errors, and use cases.
To remove a team channel member, use the HTTP DELETE method to make an API call. Below is the general API endpoint format:
DELETE https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/members/{memberId}
Run Get-MgTeam cmdlet to get the team id. Use Invoke-MgGraphRequest to fetch teams channel by calling this -Uri https://graph.microsoft.com/v1.0/teams/
# Define team and channel IDs
$teamId = "ffe1047b-bdd7-48e1-a103-56d65c783ba9"
$channelId = "19:107f0b35e7ae4af589397aed3ecf92f9@thread.tacv2"
# Get all members of the channel
$response = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/members"
# Loop through each member to extract member IDs and display names
$members = $response.value | ForEach-Object {
[PSCustomObject]@{
DisplayName = $_.displayName
MemberId = $_.id
UserId = $_.userId
}
}
# Display the information
$members | Format-Table -AutoSize
Note: The channel member ids are available within the ‘value’ key as hashtable. This hashtable has to be looped through to pull out the channel member ids [along with the user details such as Display Name and User Id]
This example removes a single member from a specific channel using the Invoke-MgGraphRequest cmdlet.
# Define team, channel, and member IDs
$teamId = "ece6f0a1-7ca4-498b-be79-edf6c8fc4d82"
$channelId = "19:56eb04e133944cf69e603c5dac2d292e@thread.skype"
$memberId = "ZWUwZjVhZTItOGJjNi00YWU1LTg0NjYtN2RhZWViYmZhMDYyIyM3Mzc2MWYwNi0yYWM5LTQ2OWMtOWYxMC0yNzlhOGNjMjY3Zjk="
# Remove the member
Invoke-MgGraphRequest -Method DELETE -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/members/$memberId"
To remove multiple members, you can loop through a list of member IDs and remove each one using Invoke-MgGraphRequest.
# Define team and channel IDs
$teamId = "ece6f0a1-7ca4-498b-be79-edf6c8fc4d82"
$channelId = "19:56eb04e133944cf69e603c5dac2d292e@thread.skype"
# List of member IDs to remove
$memberIds = @(
"ZWUwZjVhZTItOGJjNi00YWU1LTg0NjYtN2RhZWViYmZhMDYyIyM3Mzc2MWYwNi0yYWM5LTQ2OWMtOWYxMC0yNzlhOGNjMjY3Zjk="
"e3b23fa2-7f8f-493c-bc2b-ff0de3c4204e"
)
# Remove each member
foreach ($memberId in $memberIds) {
Invoke-MgGraphRequest -Method DELETE -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/members/$memberId"
}
For bulk member removal, load member details from a CSV file and process them:
# Import the CSV file
$csvData = Import-Csv -Path "C:\Path\To\Members.csv"
# Loop through each record and remove the members
foreach ($record in $csvData) {
$teamId = $record.TeamId
$channelId = $record.ChannelId
$memberId = $record.MemberId
# Remove the member
Invoke-MgGraphRequest -Method DELETE -Uri "https://graph.microsoft.com/v1.0/teams/$teamId/channels/$channelId/members/$memberId"
}
ChannelMember.ReadWrite.All permissions to remove members from the Teams channel.| Error | Cause | Solution |
| 404 Not Found | Incorrect team, channel, or member ID. | Double-check the IDs and ensure the member exists in the specified channel. |
| 403 Forbidden | Lack of permissions to remove members from the channel. | Ensure the user has ChannelMember.ReadWrite.All permissions. |
| 429 Too Many Requests | API rate limit exceeded. | Implement delays between API calls to avoid rate limiting. |
The Invoke-MgGraphRequest cmdlet offers a flexible way to manage Microsoft Teams channel membership. Whether removing single members, multiple members, or bulk members using a CSV file, this method ensures precise and efficient management of Teams memberships. By leveraging Microsoft Graph API, administrators gain more control, improving security and organization within Teams.
© m365corner.com. All Rights Reserved. Design by HTML Codex