How can I remove all members from multiple groups in AAD?

Ricardo Soares Teixeira 20 Reputation points
2023-07-14T18:47:20.3933333+00:00

Hi, how can I remove all members from multiple groups in AAD?

Example:

Group1 = 200 members

Group2 = 150 members

I want to remove all members running only one powershell comand or whatever might be the option we have to reach that. The group itself must be kept, I want to remove only members.

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
18,597 questions
{count} votes

Accepted answer
  1. Harpreet Singh Matharoo 7,376 Reputation points Microsoft Employee
    2023-07-17T08:25:31.0866667+00:00

    Hello @Ricardo Soares Teixeira

    Thank you for reaching out. You can use following PowerShell Script to remove all users from Azure AD Groups.

    Connect-AzureAD
    
    $GroupObjectID= Read-Host -Prompt "Enter Group Object ID from which you would like to remove all the members" 
    $GroupName = Get-AzureADGroup -ObjectId $GroupObjectID | Select-Object -ExpandProperty DisplayName  
    Write-Host "Group you entered is $GroupName"
    
    $Memberlist = Get-AzureADGroupMember -ObjectId $GroupObjectID -All $true 
    foreach ($user in $Memberlist) 
    { 
    Remove-AzureADGroupMember -ObjectId $GroupObjectID -MemberId $user.ObjectId 
    Write-host "Removed user account "$user.DisplayName" from group "$GroupName""
    } 
    

    Sample Output:
    User's image

    I hope this helps and fixes your issue. Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful