Fastest way to remove all users from an Azure AD group with PowerShell

5.8k Views Asked by At

What is the fastest way to remove all users from an Azure AD group in PowerShell? I am currently using

$deleteThem = Get-MsolGroupMember -GroupObjectId $groupId -All 
foreach ($user in $deleteThem) {
    Remove-MsolGroupMember -GroupObjectId $groupId -GroupMemberObjectId $user.ObjectId
}

but this is painfully slow. I need to retain the group and group id though. Any ideas?

1

There are 1 best solutions below

0
Ansuman Bal On

As mentioned by Ash in comments section , Using Remove-AzADGroupMember is faster than Remove-MsolGroupMember.

I ran the below script for deleting users in a group by just providing the Group name.

Import-Module AzureAD
$Credential = Get-Credential
Connect-AzureAD -Credential $Credential
$group=Get-AzureADGroup -SearchString 'Your Tenant Group Name' 
$users=Get-AzureADGroupMember -ObjectId $Group.ObjectId -All $true |where {$_.ObjectType -eq 'User'}
foreach($user in $users){
Remove-AzureADGroupMember -ObjectId $Group.ObjectId -MemberId $user.objectId
} 

enter image description here

Note : In credentials prompt box, provide the admin id of the tenant and password. If there are lot of users in the group then it is expected to take some time.