Forum Discussion

stogiefan's avatar
stogiefan
Brass Contributor
Feb 21, 2022

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...
  • Harm_Veenstra's avatar
    Harm_Veenstra
    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)

Resources