SOLVED

Remove bulk users from mail-enabled security group

%3CLINGO-SUB%20id%3D%22lingo-sub-3195033%22%20slang%3D%22en-US%22%3ERemove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3195033%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20have%20an%20OU%20built%20for%20Withdrawn%20students%3B%20the%20accounts%20are%20disabled%20but%20they%20still%20show%20up%20in%20groups%20they%20belonged%20to.%20We%20need%20to%20be%20able%20to%20remove%20them%20from%20mailing%20lists.%20We%20use%20a%20mail-enabled%20security%20group%20for%20All%20Students.%20I%20need%20to%20be%20able%20to%20remove%20all%20the%20Withdrawn%20students%20from%20this%20group.%20What%20is%20the%20best%20way%20to%20go%20about%20this%3F%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20in%20advance!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3195033%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3195506%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3195506%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F485323%22%20target%3D%22_blank%22%3E%40stogiefan%3C%2FA%3E%26nbsp%3BSomething%20like%20this%2C%20you%20have%20to%20enter%20the%20OU%20of%20the%20Withdrawn%20users%20in%20it%2C%20use%20a%20-whatif%20to%20test%20after%20the%20remove-adgroupmember%20%3B)%3C%2Fimg%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach%20(%24user%20in%20get-aduser%20-filter%20*%20%7C%20where-object%20DistinguishedName%20-match%20'ou%20of%20withdrawn%20students')%20%7Bget-adgroup%20-filter%20*%20-properties%20mail%20%7C%20where-object%20%7B(%24_.mail%20-ne%20%24Null)%20-and%20(%24_.groupcategory%20-eq%20%22Security%22)%7D%7D%20%7C%20remove-adgroupmember%20-members%20%24user%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3201160%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3201160%22%20slang%3D%22en-US%22%3EHi%20again%2C%20and%20thanks%20again%20for%20your%20response.%20I%20could%20not%20get%20this%20to%20work.%20I%20am%20painfully%20inexperienced%20with%20Powershell.%20But%20I%20do%20want%20to%20try%20and%20understand%20this.%3CBR%20%2F%3E%3CBR%20%2F%3EI%20know%20I%20need%20to%20identify%20the%20members%20of%20an%20OU%20in%20AD.%20I%20assume%20to%20do%20so%20I%20need%20to%20use%20the%20path%20name%20rather%20than%20just%20the%20nested%20OU's%20name.%3CBR%20%2F%3Eeg%20-%20%22(DOMAIN)%2FUSERS%2FSTUDENTS%2FWITHDRAWN%22%20...%20or%20just%20%22Withdrawn%22%3F%3CBR%20%2F%3E%3CBR%20%2F%3EAlso%2C%20where%20do%20I%20identify%20the%20group%20name%20that%20they%20need%20to%20be%20removed%20from%3F%20In%20this%20case%20it%20is%20%22Students%20-%20All%22.%3CBR%20%2F%3E%3CBR%20%2F%3EAlso%2C%20I%20assume%20I%20run%20this%20in%20powershell%20from%20my%20AD%20server%20correct%3F%3CBR%20%2F%3E%3CBR%20%2F%3EI%20apologize%20for%20so%20many%20questions%2C%20I%20may%20be%20in%20over%20my%20head!%20Ha!%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3201246%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3201246%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F485323%22%20target%3D%22_blank%22%3E%40stogiefan%3C%2FA%3E%26nbsp%3BDoesn't%20matter%2C%20you're%20using%20PowerShell%20and%20that's%20always%20ok!%20I%20thought%20you%20wanted%20the%20users%20to%20be%20removed%20from%20any%20mail-enabled%20security%20group%2C%20but%20if%20you%20only%20have%20one%20group%20that%20the%20users%20need%20to%20be%20removed%20from..%20That's%20easier%20and%20I%20replaced%20the%20OU%20filter%20for%20a%20wildcard%20search%20on%20any%20user%20within%20the%20Withdrawn%20OU%2C%20try%20the%20script%20below%20and%20if%20it%20returns%20some%20users%20that%20would%20be%20affected%20by%20it..%20You%20can%20then%20remove%20the%20-WhatIf%20part%2C%20I've%20added%20the%20-Force%3A%24True%20parameter%20so%20that%20it%20won't%20ask%20you%20if%20you%20want%20to%20remove%20the%20user%20for%20every%20occurence.%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20you%20can%20run%20it%20from%20your%20Domain%20Controller%20%2F%20AD%20Server%2C%20that's%20the%20easiest%20for%20you%20now%20I%20guess%20%3B)%3C%2Fimg%3E%20(You%20can%20install%20the%20RSAT%20tools%20on%20your%20computer%20too%20and%20run%20a%20PowerShell%20command%20from%20there%20if%20you%20start%20it%20as%20your%20Admin%20account)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E-%20edit%20-%20Changed%20%24user%20to%20%24user.SamAccountName%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach%20(%24user%20in%20get-aduser%20-filter%20*%20%7C%20where-object%20DistinguishedName%20-like%20'*Withdrawn*')%20%7BRemove-AdGroupMember%20-Identity%20'Students%20-%20All'%20-members%20%24user.SamAccountName%20-Force%3A%24True%20-WhatIf%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3203919%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3203919%22%20slang%3D%22en-US%22%3EI%20cannot%20thank%20you%20enough%20for%20working%20with%20me%20on%20this.%20I%20understand%20this%20a%20bit%20better%20now.%20When%20I%20first%20ran%20it%2C%20it%20said%20it%20did%20not%20recognize%20the%20%22force%22%20parameter.%20I%20removed%20that%20and%20the%20%22whatif%22%20and%20it%20said%20it%20could%20not%20find%20the%20Students%20-%20All%20object.%20this%20may%20be%20due%20to%20the%20hybrid%20nature%20of%20our%20environment.%20That%20OU%20exists%20in%20our%20local%20AD%20but%20the%20security%20group%20may%20be%20cloud-only.%20I%20am%20not%20sure%20how%20this%20works%20as%20I%20am%20not%20the%20network%20admin%2C%20I%20am%20the%20computer%20technician.%20But%20my%20coworker%20won't%20do%20this%20stuff%20so%20I%20am%20forced%20to%20try%20and%20learn%20it.%3CBR%20%2F%3EThank%20you%20again%20for%20taking%20your%20time%20to%20help%20me!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3203957%22%20slang%3D%22en-US%22%3ERe%3A%20Remove%20bulk%20users%20from%20mail-enabled%20security%20group%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3203957%22%20slang%3D%22en-US%22%3EOk%2C%20that%20might%20be%20the%20case...%20You%20could%20try%20to%20change%20remove-adgroupmember%20to%20Remove-DistributionGroupMember%2C%20the%20parameters%20are%20the%20same..%20If%20you%20do%20a%20seach%20in%20Active%20Directory%20Users%20and%20Computers%2C%20does%20it%20show%20go%20anything%20while%20searching%20for%20Students%3F%20It%20could%20also%20be%20a%20dynamic%20group%2C%20that%20users%20are%20put%20in%20it%20automatically.%20In%20that%20case%2C%20the%20query%20behind%20it%20should%20be%20modified%20perhaps%20to%20include%20enabled%20users%3F%3C%2FLINGO-BODY%3E
Occasional Contributor

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 security group for All Students. I need to be able to remove all the Withdrawn students from this group. What is the best way to go about this? 

Thank you in advance!

17 Replies

@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 }

 

Wow, thanks so much for this. I will get back to you after giving it a try!
Hi again, and thanks again for your response. I could not get this to work. I am painfully inexperienced with Powershell. But I do want to try and understand this.

I know I need to identify the members of an OU in AD. I assume to do so I need to use the path name rather than just the nested OU's name.
eg - "(DOMAIN)/USERS/STUDENTS/WITHDRAWN" ... or just "Withdrawn"?

Also, where do I identify the group name that they need to be removed from? In this case it is "Students - All".

Also, I assume I run this in powershell from my AD server correct?

I apologize for so many questions, I may be in over my head! Ha!

@stogiefan Doesn't matter, you're using PowerShell and that's always ok! I thought you wanted the users to be removed from any mail-enabled security group, but if you only have one group that the users need to be removed from.. That's easier and I replaced the OU filter for a wildcard search on any user within the Withdrawn OU, try the script below and if it returns some users that would be affected by it.. You can then remove the -WhatIf part, I've added the -Force:$True parameter so that it won't ask you if you want to remove the user for every occurence.

And you can run it from your Domain Controller / AD Server, that's the easiest for you now I guess ;) (You can install the RSAT tools on your computer too and run a PowerShell command from there if you start it as your Admin account)

 

- edit - Changed $user to $user.SamAccountName

 

 

foreach ($user in get-aduser -filter * | where-object DistinguishedName -like '*Withdrawn*') {Remove-AdGroupMember -Identity 'Students - All' -members $user.SamAccountName -Force:$True -WhatIf }

 

 

 

 

I cannot thank you enough for working with me on this. I understand this a bit better now. When I first ran it, it said it did not recognize the "force" parameter. I removed that and the "whatif" and it said it could not find the Students - All object. this may be due to the hybrid nature of our environment. That OU exists in our local AD but the security group may be cloud-only. I am not sure how this works as I am not the network admin, I am the computer technician. But my coworker won't do this stuff so I am forced to try and learn it.
Thank you again for taking your time to help me!
Ok, that might be the case... You could try to change remove-adgroupmember to Remove-DistributionGroupMember, the parameters are the same.. If you do a seach in Active Directory Users and Computers, does it show go anything while searching for Students? It could also be a dynamic group, that users are put in it automatically. In that case, the query behind it should be modified perhaps to include enabled users?
That didn't work out either. Let me give you more info and it may help.
Ad is on our DC and Exchange is on it's own server. Then we have the cloud environment (Azure and 365). I am not sure how that all works together. The users accounts are in the OU I mentioned above and they are disabled (since they are withdrawn students). For whatever reason the school wants to keep the accts but remove them from distros. We use several diff groups for those distros. In this case, "Students - All" is a mail-enabled security group.
I was hoping I could somehow identify the OU members (withdrawn students) to remove them in bulk from distros (specifically the one above).
Does this help?
I am so appreciative of you taking your time here. I completely understand if this becomes annoying, lol.

@stogiefan If it's really a Active Directory group, mail-enabled security, could I say that every disabled user in it should be removed from it? Could make thing somewhat easier :) 

I am so sorry I am having trouble understanding.
I think I just figured something out. In our local AD, on the DC, there are only Computers and Users. I don't see any groups.
In Azure AD I see all the groups. That may be the disconnect.
To answer your question, yes, all the users in the Withdrawn OU are disabled and they are the ones that need to be removed from it.
Hope this helps!

@stogiefan Ok, well.. Here's another try, you can run it on your DC. First you install the module ExchangeOnlineManagement and connect to your 365 environment. (If you're not an Exchange Admin or Global Admin online, this won't work). Then you run through the users again and remove them from the online distribution-group. Remove the -whatif if you get the users returned that it would remove from the group..

 

 

Install-Module ExchangeOnlineManagement -Scope CurrentUser
Connect-ExchangeOnline
foreach ($user in get-aduser -filter * | where-object DistinguishedName -like '*Withdrawn*') {Remove-DistributionGroupMember -Identity 'Students - All' -member $user.SamAccountName -Confirm:$False -WhatIf }

 

 

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'.
best response confirmed by stogiefan (Occasional Contributor)
Solution

@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)

YES!! This is it, thank you so much!!
Nice! 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
I will and thank you again!
That 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"
I understand, but formatting was difficult and this was easier in this case :grinning_face_with_sweat: