Answered by:
Bulk account removal from a group

Question
-
How can you bulk remove accounts from a mail-enabled group using Powershell? We have some groups with high membership counts for which we need to remove hundreds of accounts (there will still be thousands of other accounts remaining in the groups). The majority of these user accounts no longer have mailboxes. We have the dn and samAccountName for all of these user objects that need to be removed.Thursday, December 22, 2011 7:15 PM
Answers
-
On Fri, 23 Dec 2011 20:55:09 +0000, Viministrator wrote:>$User = read-host -Prompt "Enter Username" "User " + $User + " is deleted from following groups:" $DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.Name}) ?eq $User } foreach($DG in $Dgs) {Remove-DistributionGroupMember $Dg -member $user -Confirm:$false }He said he had hundreds of DNs to remove so entering the informationby hand would be insane (or he'd be insane by the time he was done)!He didn't say that the groups were mail-enabled, either -- soremove-distributiongroupmember won't work (besides being very slow).---Rich MatheisenMCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP- Proposed as answer by Zi FengModerator Thursday, December 29, 2011 2:34 AM
- Marked as answer by Simon_WuMicrosoft contingent staff, Moderator Friday, December 30, 2011 9:26 AM
Friday, December 23, 2011 10:06 PM -
Hi
I suggest put all DNs or samAccoutName which need to be removed in a csv file, then using import-csv command to import all the DNs in,and then run the script。
Cheers
Zi Feng
- Proposed as answer by Zi FengModerator Thursday, December 29, 2011 2:34 AM
- Marked as answer by Simon_WuMicrosoft contingent staff, Moderator Friday, December 30, 2011 9:26 AM
Monday, December 26, 2011 2:55 AMModerator
All replies
-
On Thu, 22 Dec 2011 19:15:05 +0000, davrion wrote:>How can you bulk remove accounts from a mail-enabled group using Powershell? We have some groups with high membership counts for which we need to remove hundreds of accounts (there will still be thousands of other accounts remaining in the groups). The majority of these user accounts no longer have mailboxes. We have the dn and samAccountName for all of these user objects that need to be removed.Something like this should work:$g = get-group <NAME>$m = $g.members$d | foreach{if ($m -contains $_){$m -= $_}}$g.members = $m$g.setinfo()---Rich MatheisenMCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVPFriday, December 23, 2011 2:40 AM -
$User = read-host -Prompt "Enter Username"
"User " + $User + " is deleted from following groups:"
$DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.Name}) –eq $User }
foreach($DG in $Dgs)
{Remove-DistributionGroupMember $Dg -member $user -Confirm:$false }save it as .PS1
Where Technology Meets TalentFriday, December 23, 2011 8:55 PM -
On Fri, 23 Dec 2011 02:40:07 +0000, Rich Matheisen [MVP] wrote:>>$d =DOH!That 1st line should have been$d = DN1, DN2, DN3, etc.If you have the distinguishednames in a file then:$d = (get-content <FILE>) | foreach{$dn=$_.trim()if ($dn.length -gt 1){$dn}}>$g = get-group <NAME>>$m = $g.members>$d | foreach{>if ($m -contains $_)>{>$m -= $_>}>}>$g.members = $m>$g.setinfo()If you want to examine EVERY group in your AD:$d = <LOAD IT WITH THE DN's>get-group -resultsize unlimited | foreach {$g = $_$m = $g.members$d | foreach{if ($m -contains $_){$m -= $_}}$g.members = $m$g.setinfo()}---Rich MatheisenMCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVPFriday, December 23, 2011 10:02 PM -
On Fri, 23 Dec 2011 20:55:09 +0000, Viministrator wrote:>$User = read-host -Prompt "Enter Username" "User " + $User + " is deleted from following groups:" $DGs= Get-DistributionGroup | where { (Get-DistributionGroupMember $_ | foreach {$_.Name}) ?eq $User } foreach($DG in $Dgs) {Remove-DistributionGroupMember $Dg -member $user -Confirm:$false }He said he had hundreds of DNs to remove so entering the informationby hand would be insane (or he'd be insane by the time he was done)!He didn't say that the groups were mail-enabled, either -- soremove-distributiongroupmember won't work (besides being very slow).---Rich MatheisenMCSE+I, Exchange MVP
--- Rich Matheisen MCSE+I, Exchange MVP- Proposed as answer by Zi FengModerator Thursday, December 29, 2011 2:34 AM
- Marked as answer by Simon_WuMicrosoft contingent staff, Moderator Friday, December 30, 2011 9:26 AM
Friday, December 23, 2011 10:06 PM -
Hi
I suggest put all DNs or samAccoutName which need to be removed in a csv file, then using import-csv command to import all the DNs in,and then run the script。
Cheers
Zi Feng
- Proposed as answer by Zi FengModerator Thursday, December 29, 2011 2:34 AM
- Marked as answer by Simon_WuMicrosoft contingent staff, Moderator Friday, December 30, 2011 9:26 AM
Monday, December 26, 2011 2:55 AMModerator -
Hi
Any update?
Cheers
Zi Feng
Wednesday, December 28, 2011 5:32 AMModerator