Remove user from all Microsoft 365 groups

  • Last update on September 26th, 2024

Table of Contents

Custom actions are not available in the Essentials solution.

 

If you need to remove a user membership from all Microsoft 365 groups, you can import the following script in a CoreView custom action.

Custom action script:

{
 "id": "1eba1dd4-e697-42b7-85d8-85acdd40e2c5",
 "title": "Exchange - Remove a user from all M365 groups",
 "description": "Remove a user's complete memberships from all M365 groups",
 "lastModified": "2022-05-16T13:11:59.3820000Z",
 "target": "None",
 "tags": [],
 "vars": [
 {
 "name": "UserPrincipalName",
 "type": "string",
 "isRequired": true
 }
 ],
 "params": [],
 "columns": {},
 "version": 4,
 "statement": "param ([string]$UserPrincipalName)\r\n\r\n$user = Get-AzureADUser -ObjectId $UserPrincipalName\n\n$memberships = Get-AzureADUserMembership -ObjectId $user.ObjectId | Where-Object {$_.ObjectType -eq \"Group\"}\n\nForEach ($group in $memberships){\n Remove-AzureADGroupMember -ObjectId $group.ObjectId -MemberId $user.ObjectId\n}"
}