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)
stogiefan
Feb 23, 2022Brass Contributor
Thank you yet again!!
I am able to connect to exch online but I got this error:
A parameter cannot be found that matches parameter name 'members'.
I am able to connect to exch online but I got this error:
A parameter cannot be found that matches parameter name 'members'.
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 25, 2022I understand, but formatting was difficult and this was easier in this case 😅
- Newbie_JonesFeb 25, 2022Brass ContributorThat seems like an expensive (performance wise) way of doing this.
Getting all AD Users and then filtering locally (Where-Object) for using a wildcard search where you can just use the search base to get the users from the Withdrawn container.
Get-ADUser -Filter * -Searchbase "OU=Withdrawn,OU=xxx,DC=xx,DC=xx,DC=xx" - stogiefanFeb 23, 2022Brass ContributorI will and thank you again!
- Feb 23, 2022Nice! No problem, glad this worked out for you! Perhaps following an online training is something for you, PowerShell can help you with some many things and sometimes there's no escaping it 😉 https://www.youtube.com/watch?v=CbAbULaEok4
- stogiefanFeb 23, 2022Brass ContributorYES!! This is it, thank you so much!!