Remove a User from all M365 Groups using PowerShell

Modified on Thu, 03 Nov 2022 at 09:54 AM

If you are looking for a way to remove a user's membership on all M365 groups, you could import the following script: 


In Coreview:

 

{
   "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}"
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article