Remove Multiple users from AD Security group

Welcome Forums General PowerShell Q&A Remove Multiple users from AD Security group

This topic contains 3 replies, has 3 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #91790

    Points: 0
    Rank: Member

    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
  • #91792

    Points: 67
    Rank: Member

    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}
  • #91793

    Points: 711
    Helping Hand
    Rank: Major Contributor

    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.

  • #91796

    Points: 0
    Rank: Member

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

The topic ‘Remove Multiple users from AD Security group’ is closed to new replies.