Forum Discussion
stogiefan
Feb 21, 2022Brass Contributor
Remove bulk users from mail-enabled security group
We have an OU built for Withdrawn students; the accounts are disabled but they still show up in groups they belonged to. We need to be able to remove them from mailing lists. We use a mail-enabled se...
- Feb 23, 2022
stogiefan My bad, should have been member instead of members.. Try this:
foreach ($user in get-aduser -filter * | where-object DistinguishedName -like '*Withdrawn*') {Remove-DistributionGroupMember -Identity 'Students - All' -member $user.SamAccountName -Confirm:$False -WhatIf }
(You don't have to install the module again or connect-exchangeonline again in your session, next session you can connect straight away without installing the module too)
Feb 21, 2022
stogiefan Something like this, you have to enter the OU of the Withdrawn users in it, use a -whatif to test after the remove-adgroupmember 😉
foreach ($user in get-aduser -filter * | where-object DistinguishedName -match 'ou of withdrawn students') {get-adgroup -filter * -properties mail | where-object {($_.mail -ne $Null) -and ($_.groupcategory -eq "Security")}} | remove-adgroupmember -members $user }
stogiefan
Feb 22, 2022Brass Contributor
Wow, thanks so much for this. I will get back to you after giving it a try!