简体   繁体   中英

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

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?

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
} 

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM