none
Adding Multiple users to Multiple AD Groups RRS feed

  • Question

  • I have a .csv file with 2 columns. Sam and Newgroup

    Column 1 -Sam is the SAM account name in AD for the user I am trying to add into the groups.

    Column 2 -Groups column has multiple groups in a single cell separated by ; Like this. test1;test2;test3

    Code I am working with is this

    $Users=import-csv-Path"C:\filename.csv"  -delimiter';'


    foreach ($User in $Users) {

    $SAM = $user.Sam

    $Group = $User.newgroup
    Add-ADGroupMember - Identity $Group -Members $SAM

    }

    Error is as follows

    Add-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument is null.

    New to powershell so a little lost


    }

    Monday, April 29, 2019 12:38 PM

Answers

  • Try this:

    $Users = Import-Csv -LiteralPath D:\NewUserGroups.csv
    
    ForEach ($User in $Users) {
    
        $Username = $User.Sam
        $Groups = $User.Newgroup.Split(';')
    
        ForEach ($Group in $Groups) {
        
            Add-ADGroupMember -Identity $Group -Members $Username
        
        }
    
    }




    • Proposed as answer by Simon Cutting Tuesday, April 30, 2019 5:25 PM
    • Edited by Simon Cutting Tuesday, April 30, 2019 5:26 PM
    • Marked as answer by RussHaight Friday, May 3, 2019 12:51 PM
    Tuesday, April 30, 2019 5:23 PM

All replies

  • A Csv file cannot have two column headers and more than two columns.  It will not work.

    You have to use a different delimiter for the groups or use a different delimiter for the columns or place quotes around column 2.

    You cannot use a list of groups to add a user.  You can only add to one group at a time.

    See:

    help Add-ADPrincipalGroupMembership -online


    \_(ツ)_/


    • Edited by jrv Monday, April 29, 2019 1:01 PM
    Monday, April 29, 2019 12:57 PM
  • Try this:

    $Users = Import-Csv -LiteralPath D:\NewUserGroups.csv
    
    ForEach ($User in $Users) {
    
        $Username = $User.Sam
        $Groups = $User.Newgroup.Split(';')
    
        ForEach ($Group in $Groups) {
        
            Add-ADGroupMember -Identity $Group -Members $Username
        
        }
    
    }




    • Proposed as answer by Simon Cutting Tuesday, April 30, 2019 5:25 PM
    • Edited by Simon Cutting Tuesday, April 30, 2019 5:26 PM
    • Marked as answer by RussHaight Friday, May 3, 2019 12:51 PM
    Tuesday, April 30, 2019 5:23 PM
  • Thank you-

    work perfectly but you probably already knew that :)

    Friday, May 3, 2019 12:52 PM