Forum Discussion
rhupf
Jan 08, 2024Brass Contributor
How to remove a user from all Teams with Graph SDK Powershell
I have a script that I run for terminated accounts that has this line to remove the user from all Teams they are a member of. get-team -User $username | foreach {Remove-TeamUser -GroupId $_.group...
hustler0109
Jan 08, 2024Copper Contributor
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All"
# Define the user's email address and retrieve their Teams memberships
$userEmail = "email address removed for privacy reasons"
$user = Get-MgUser -Filter "userPrincipalName eq '$userEmail'"
$userTeams = Get-MgUserJoinedTeams -UserId $user.Id
# Iterate through each Team and remove the user
foreach ($team in $userTeams) {
Remove-MgTeamUser -GroupId $team.Id -UserId $user.Id
}
I hope this helps!
Connect-MgGraph -Scopes "Group.ReadWrite.All"
# Define the user's email address and retrieve their Teams memberships
$userEmail = "email address removed for privacy reasons"
$user = Get-MgUser -Filter "userPrincipalName eq '$userEmail'"
$userTeams = Get-MgUserJoinedTeams -UserId $user.Id
# Iterate through each Team and remove the user
foreach ($team in $userTeams) {
Remove-MgTeamUser -GroupId $team.Id -UserId $user.Id
}
I hope this helps!
rhupf
Jan 08, 2024Brass Contributor
Thanks for the try.
I get the error: Remove-MgTeamUser : The term 'Remove-MgTeamUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
The command I see is Remove-MgTeamMember, but it doesn't accept the $user.id param
+ Remove-MgTeamMember -teamId $team.Id -conversationmemberId $user.Id
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ({ TeamId = 5e70...c, IfMatch = }:<>f__AnonymousType287`3) [Remove-MgTeamMember_Delete], Exception
+ FullyQualifiedErrorId : BadRequest,Microsoft.Graph.PowerShell.Cmdlets.RemoveMgTeamMember_Delete
I get the error: Remove-MgTeamUser : The term 'Remove-MgTeamUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
The command I see is Remove-MgTeamMember, but it doesn't accept the $user.id param
+ Remove-MgTeamMember -teamId $team.Id -conversationmemberId $user.Id
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ({ TeamId = 5e70...c, IfMatch = }:<>f__AnonymousType287`3) [Remove-MgTeamMember_Delete], Exception
+ FullyQualifiedErrorId : BadRequest,Microsoft.Graph.PowerShell.Cmdlets.RemoveMgTeamMember_Delete
- rhupfJun 05, 2024Brass ContributorIt's been a while, but I have yet to figure out a solution to this, so I thought I'd repost in case someone new notices it and has an idea. For now, my only option is to use the old Teams module method:
get-team -User $email address removed for privacy reasons | foreach {Remove-TeamUser -GroupId $_.groupid -User $email address removed for privacy reasons}
I'd like to get this script updated to use the Graph SDK Powershell module.- davmasterAug 08, 2024Copper ContributorYou need to use the conversation member id, not the user id.
$convMemberID = (Get-MgTeamMember -TeamId $team.Id -Filter "(microsoft.graph.aadUserConversationMember/userId eq '$mgUserId')").Id
if($convMemberID -ne $null){Remove-MgTeamMember -TeamId $team.Id -ConversationMemberId $convMemberID}- rhupfAug 13, 2024Brass ContributorI tried this, but no luck. I get the following error message:
$convmemberid = (get-mgteammember -teamid "5e70225a-2955-42d7-a950-3daa475d9e19" -Filter "(microsoft.graph.aadUserConversationMember/userId eq 'email address removed for privacy reasons')").id
get-mgteammember : Invalid id 'email address removed for privacy reasons' provided.
Status: 400 (BadRequest)