Bulk add users to groups

Hi Folks

Relatively new to Powershell and I have Googled this for a while with no solution.

I am sure it will be basic to some on here.

I am looking to create a script that utilises a CSV file and allows me to add multiple users to multiple groups.

So far I have

Import-Csv “C:\Scripts\ServiceDesk\BulkGroupAdd\BulkGroupAdd.csv” | ForEach-Object {Add-ADGroupMember -Identity $.GroupName -Members $.UserName}7

My csv file headers are

GroupName,UserName

Using this script I can add users to one group.

I thought I would be able to add multiple groups into the GroupName field like below

“group1,group2,group3”

This fails however and reports that it cannot locate “group1,group2,group3”

If I just add one group like below the user is added to that group

group1

I am assuming that something needs to be modified in the actual script.

If this makes sense could someone very kindly take the time to help?

ok, you have a bit of a misunderstanding of how add-adgroupmember works.

add-adgroupmember only touches one ad group at a time.
you can however add multiple users to the same group with a single call by using a comma seperated list.

however as add-adgroupmember accepts pipeline input, you might be able to do something like:

Import-Csv "C:\Scripts\ServiceDesk\BulkGroupAdd\BulkGroupAdd.csv" | ForEach-Object {$_.GroupName|Add-ADGroupMember -Members  $_.UserName}

i don’t have access to a shell at the moment… but you can give that a try

Thanks David I will give that a try

So are we saying that its not possible to add multiple users to multiple groups?

So if I have a spreadsheet with users on it I can’t add all of those users to 3 different groups at the one time?

I would have to run 3 different commands one for each group?

Apologies if I am not understanding.

I came across this which is a different approach but seems to be the kind of thing I am looking for.

Tough learning curve this powershell stuff lol :slight_smile:

well, another post that was submitted earlier made me aware of a cmdlet i wasn’t before.

Add-ADPrincipalGroupMembership
Adds a member to one or more Active Directory groups.

i haven’t used it before, but if you look at the post “adding computer to multiple ad groups” there are usage examples there