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 $_.groupid -User $email address removed for privacy reasons}
I've been working on upgrading our scripts to user the Graph SDK Powershell, but I can't figure out a way to convert this command to one that works. Any thoughts?
5 Replies
- hustler0109Copper 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!- rhupfBrass ContributorThanks 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- rhupfBrass 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.