Share via


Add user to multiple groups

Question

Sunday, October 27, 2013 12:12 AM

Hi there folks, I'm trying to bulk add users into multiple groups.
I know this has been discussed before, but none of the other examples seem to work for me.
The one closest to my wishes is https://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/4f3efb65-6f13-4519-957f-d053e6f93888/
But this script needs users either to be entered in an array or retrieved from a file with 'gc'.
But then again, you've got two places containing related data. if i do the same thing with below command for single group it works  not for mutliple groups

My csv file has two  tabs Users group

for e.g

users,group
_SptLamone_50,"ACL_ONE50_NONDATROLES_MACHINEUSER   ACL_ONE50_CUSTDATROLE_MACHINEUSER"

What I need is something like this:

Import-module ActiveDirectory
Import-Csv C:\Temp\UsersInGroups.csv | foreach{Add-ADGroupMember -Identity $_.GroupName -Member $_.User

All replies (2)

Sunday, October 27, 2013 12:28 AM âś…Answered

I got the answer from this link  i had to only modified  the split part (see the bold )

http://social.technet.microsoft.com/Forums/windowsserver/en-US/baa556ad-ef54-4857-885f-b3dcd1caa03c/add-multiple-user-in-multiple-groups-from-csv-file?forum=ITCG

$Items = import-csv .\UserList.csv
$Items | ForEach-Object {
  $SamAccountName = $_.SamAccountName
  $Groups = ($_.Groups).split(" ")
  foreach ($Group in $Groups)
  {
    "Adding $SamAccountName to the following group: $Group"
     Add-ADGroupMember -Identity $Group $SamAccountName
  }      
}

Thanks


Monday, October 28, 2013 5:55 AM | 1 vote

Hi,

Thanks for your sharing the solution!

Have a nice day!

Regards,

Yan Li

TechNet Subscriber Support

If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time.
Thanks for helping make community forums a great place.