Bulk add users to groups

This topic contains 4 replies, has 2 voices, and was last updated by  David Schmidtberger 5 months, 3 weeks ago.

  • Author
    Posts
  • #69486

    Daniel Birrell
    Participant

    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?

  • #69498

    David Schmidtberger
    Participant

    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

    • #69502

      Daniel Birrell
      Participant

      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.

    • #69504

      Daniel Birrell
      Participant

      https://www.sapien.com/forums/viewtopic.php?t=9752

      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 🙂

  • #69556

    David Schmidtberger
    Participant

    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

You must be logged in to reply to this topic.