none
Add Multiple Users to Multiple Groups in AD via csv file RRS feed

  • Question

  • I'm trying to add multiple users to multiple groups from a csv file and have looked through every post about adding multiple users to multiple groups for the past weeks but due to the nature of csv file I am having trouble executing it correctly.

    The data in the csv file has 5 headings (username, fac1, fac2, fac3, fac4)

    What I have so far

    Import-Module ActiveDirectory
    
    $faculties = @("FACULTY01", "FACULTY02", "FACULTY03", "FACULTY04")
    
    $csv = Import-csv C:\TestADGroupADD.csv -Header @("Username") + $faculties
    
    
    foreach ($user in $csv | Select-Object -Skip 1)
    {
        foreach ($faculty in $faculties)
        {
            if ($user.$faculty)
            {
                Add-ADGroupMember -Identity $user.$faculty -Member $user.Username
            }
        }
    }

    I'm having trouble creating the variable '$csv' with the variable '$faculties' and getting error : Import-Csv : A positional parameter cannot be found that accepts argument 'System.Object[]'.

    Appreciate your help


    • Edited by Tech_NeQ Monday, June 17, 2019 10:39 PM Added Code Block
    Monday, June 17, 2019 3:11 AM

All replies

  • Please take a look at the cmdlet Add-ADPrincipalGroupMembership. It would make your task somewhat easier I think.  ;-)   (You can add one user to multiple groups at a time)

    If the CSV file already has headers you don't need to provide them in your code - you can use them right away. You might re-read the help for Import-Csv including the examples to learn how to use it.

    And BTW: Please format your code as code using the code posting tool provided on the icon bar of the post editor (second to last icon). Thanks.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''


    Monday, June 17, 2019 6:30 AM
  • Hi,

    Thanks for your question.

    You don't need to use -header parameter.

    $csv = Import-csv C:\TestADGroupADD.csv

    -Header

    Specifies an alternate column header row for the imported file. The column header determines the property names of the objects created by Import-Csv.

    Enter column headers as a comma-separated list. Do not enclose the header string in quotation marks. Enclose each column header in single quotation marks.

    If you enter fewer column headers than there are data columns, the remaining data columns are discarded. If you enter more column headers than there are data columns, the additional column headers are created with empty data columns.

    When using the Header parameter, delete the original header row from the CSV file. Otherwise, Import-Csvcreates an extra object from the items in the header row.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/import-csv?view=powershell-6

    Best regards,

    Lee


    Just do it.

    Monday, June 17, 2019 6:43 AM
    Moderator
  • You just need one more set of parens.

    $csv = Import-csv C:\TestADGroupADD.csv -Header (@("Username") + $faculties)

    This forces correct evaluation of the array operation.

    Your method of creating an alternate header is correct if you skip the first row (old header).


    \_(ツ)_/


    Monday, June 17, 2019 9:56 AM
    Moderator
  • This would be a way to do this cleanly:

    $faculties = @('FACULTY01', 'FACULTY02', 'FACULTY03', 'FACULTY04')
    $header = @('Username') + $faculties
    Import-csv C:\TestADGroupADD.csv -Header $header | 
        Select-Object -Skip 1 |
        ForEach-Object{
            $groups = $_ | Select-Object -Expand faculty* 
            Add-ADPrincipalGroupMembership $_.Username -MemberOf $groups
        }


    \_(ツ)_/

    Monday, June 17, 2019 10:05 AM
    Moderator
  • Thanks, that got rid of the error!
    Monday, June 17, 2019 11:00 PM
  • This would be a way to do this cleanly:

    $faculties = @('FACULTY01', 'FACULTY02', 'FACULTY03', 'FACULTY04')
    $header = @('Username') + $faculties
    Import-csv C:\TestADGroupADD.csv -Header $header | 
        Select-Object -Skip 1 |
        ForEach-Object{
            $groups = $_ | Select-Object -Expand faculty* 
            Add-ADPrincipalGroupMembership $_.Username -MemberOf $groups
        }


    \_(ツ)_/

    I tried to use this as its a cleaner way but I am getting errors:

    "Select-Object : Multiple properties cannot be expanded @  $groups = $_ | Select-Object -Expandproperty faculty*

    And also "Cannot validate argument on parameter 'MemberOf'" @ Add-ADPrincipalGroupMembership $_.Username -MemberOf $groups :/


    • Edited by Tech_NeQ Monday, June 17, 2019 11:03 PM
    Monday, June 17, 2019 11:02 PM
  • Hi,

    Thanks for your question.

    Expandproperty parameter is used to dispaly each value of array in output. You can not expand multiple attributes at a time.

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-object?view=powershell-6

    Best regards,

    Lee


    Just do it.


    Wednesday, June 19, 2019 2:57 AM
    Moderator