KB ID 0001475
Problem
I had to do this a few weeks ago, so I documented it. I had a list of usernames in a CSV file and I needed to bulk-add them to a security group.
Bulk Add Group Users Solution
Firstly you will need the usernames (sAMAccountNames) in .csv format like so, (Note: As a header Im using User-Name.) I’ve saved the file to C:\Temp on my server.
Execute the following commands;
Import-Module ActiveDirectory
Import-Csv -Path “C:\Temp\Users-To-Add.csv” | ForEach-Object {Add-ADGroupMember -Identity “Group-Name” -Members $_.’User-Name’}
And there’s our users;
Bulk Remove Group Users Solution
Use the following command;
Now if we check the group, the users have gone;
Related Articles, References, Credits, or External Links
PowerShell: Bulk Enable / Disable Users
Exchange Bulk Export / Import Mail Contacts
Bulk Export Users From One Domain, and Import Into Another
PowerShell: Add All Members of an OU to a Security Group
21/05/2019
What if i wanted to add users to multiple groups . For example if i had a list of sam account names and they needed to be addedd to multiple groups
22/05/2019
Hi,
Assuming you have Groups in a column called ‘Group’, and Users in a column called ‘User-Name’;
$list = import-csv “C:\Folder\Input-File.csv”
Foreach($user in $list){
add-adgroupmember -identity $user.Group -member (Get-ADUser $user.User-Name)
}
Pete
28/04/2021
This does not work, unless you enter the user-name in the column next to every single group you want it added to. Otherwise this will only add the user to the first group in the list. Error:
Get-ADUser : Cannot validate argument on parameter ‘Identity’. The Identity property on the argument is null or empty.
Do you know how to fix this? Thanks!
29/04/2021
>>Assuming you have Groups in a column called ‘Group’,
P
31/05/2019
I’m trying to do something similar. I have a list of AD groups I would like to remove all users from. I can do them individually, but can’t seem to get the format down to import a txt/cvs list of the groups, and then delete all the users from those groups. Any help would be appreciated.
04/11/2019
I get an error, why is that, ive followed your instructions.
PS C:\temp> Import-Csv -Path “C:\Temp\Users-To-Remove.xlsx” | ForEach-Object {Remove-ADGroupMember -Identity “ESB Admins” -Member $_.’User-Name’ -Confirm:$false}
Remove-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:119
+ … dmins” -Member $_.’User-Name’ -Confirm:$false}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
Remove-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:119
+ … dmins” -Member $_.’User-Name’ -Confirm:$false}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
Remove-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:119
+ … dmins” -Member $_.’User-Name’ -Confirm:$false}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Remove-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroupMember
04/11/2019
1. Make sure that the users in you CSV are actually in ‘ESB Admins’.
2. Make sure on their user account that they have a SAMAccountName and/or an entry under username.
11/02/2020
Add an s to the member clause “members”
13/02/2020
Sorry Matt, I can’t see what you mean?
P
23/02/2020
Add-ADGroupMember and Remove-ADGroupMember both are using the parameter -Members.
So, just a typo (missing “s”) in the examples above.
Otherwise it works great. Thank you for this posting.
24/02/2020
Hi Martin, unless the syntax has changed, then it’s using Member without an ‘s’ look at the screenshots?
P
04/03/2020
Hi Guys
just ran into the same problem and discovered that Martin is right. Changed -Member to -Members and everything worked fine (Windows Server 2019).
04/03/2020
Hi Patrick, Thanks updated accordingly
24/06/2021
It is completely unneccesary to loop over them as it accepts an array of users.
$users = Get-Aduser -filter ”
Add-ADGroupMember -members $users
22/07/2020
Very helpful thanks Pete!
07/10/2020
Also your input file extension is .xlsx and not.csv
03/12/2020
This is a great. Unfortunately it doesn’t work for users within child domains only users within current Domain. Is there any way of getting this method to work through entire forest?
24/03/2021
This is the easiest working PS command. Thank you.
15/06/2021
I have a list of users and need to remove them from all the AD groups. how to replace group name with any group?
18/03/2022
I am looking to add just under 100 users to a group, but when I try using this script I’m getting an error:
Add-ADGroupMember : Cannot validate argument on parameter ‘Members’. The argument is null or empty. Provide an argument that is not null or empty,
and then try the command again.
At line:3 char:109
+ … ADGroupMember -Identity “License-O365-Basic” -Members $_.’User-Name’}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
I am still very new to PowerShell and apologize if this is actually a simple fix. I’m just on a time constraint and could really use some assistance to figure out what is wrong. Any assistance is greatly appreciated.
21/03/2022
This is telling you that Members is returning ‘Null’. So nothing is getting ‘read in’.
20/12/2022
Hello Everyone,
We are currently cleaning our AD environment and I need a Powershell script that find AD groups that have only Disabled users as members .
Can anyone please help me with the script
16/02/2023
Great!
06/06/2023
Great, Works perfectly fine !!