Remove Multiple users from AD Security group

Hey Everyone,

I need to remove a number of users from a security group, but not all the users. My plan was to use remove-adgroupmember. I tried to use both a text file with get-content and a csv with import-csv. It’s a modified script I found to add users. I get an error message stating ‘Remove-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null or empty,’

Here’s my code, any help would be appreciated.


Import-module ActiveDirectory

$List = get-content c:\scripts\userlist2.txt

ForEach($User in $List)

remove-ADGroupMember -Identity GROUPNAME -Members $User.samaccountname -Confirm:$false

I’m assuming you have a header of samaccountname in your txt file? Get rid of it and just have a list of the samaccountname’s and modify code to this:

remove-ADGroupMember -Identity GROUPNAME -Members $_ -confirm:$false

You could also simplify the code down to one line:

gc C:\scripts\userlist2.txt | % {remove-adgroupmember -identity groupname -members $_ -confirm $false}

Actually you don’t need a loop for that. If you have an array with the users to remove in your variable $List

Remove-ADGroupMember -Identity GROUPNAME -Members $list -Confirm:$false
… should be enough.

Thanks for the Quick response. Both ways work and I saved about two days of work. Thanks again!