Populate an AD group from CSV RRS feed

  • Question

  • Let me 1st say I am a new to scripting so bear with me.

    I have a CSV file with userids that I would like to populate an AD group with. The CSV file is recreated every morning (current employees). I can run each of the commands separately and it works but as soon as I put both of my commands in one ps1 file it does not run properly. So 1st I need to remove all users from the GROUP then populate it using the CSV file. Can someone tell me what is missing?

    Import-Module ActiveDirectry; Remove-ADGroupMember "GROUP" -Members (Get-ADGroupMember "GROUP") -Confirm:$false; Import-Csv -Path "C:\temp\ppl.csv" | ForEach-Object {Add-ADGroupMember -Identity "GROUP" -Members $_.’userid’}

    In PowerShell it tells me; 


    Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an
    argument that is not null or empty, and then try the command again.
    At C:\temp\Active_Staff\Populate_AD_Group_Staff.ps1:1 char:312
    + ... Member -Identity "UCF Active Faculty and Staff" -Members $_.’userid ...
    +                                                              ~~~~
        + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGrou

    Thanks, Ulf

    Tuesday, February 11, 2020 3:10 PM

All replies

  • Please format and post your code correctly.  What you have posted is unreadable.

    Look in the Gallery for scripts that do what you are asking.  There are a few and they have been tested.


    Tuesday, February 11, 2020 3:19 PM
  • Here is an article that will show you how to corr4ectly write a script and how to format and structure the code.

    The PowerShell Best Practices and Style Guide


    Tuesday, February 11, 2020 3:25 PM
  • ... and since Add-ADGroupMember can process an array of new members you wouldn't need a Foreach-Object loop. Simply provide the desired new members as array
    $CSVData = Import-Csv -Path 'C:\temp\ppl.csv'
    Add-ADGroupMember -Identity 'GROUP' -Members $CSVData.userid
    Please always read the complete help including the examples for the cmdlets you're about to use to learn how to use them.

    Live long and prosper!


    • Edited by BOfH-666 Tuesday, February 11, 2020 3:36 PM
    Tuesday, February 11, 2020 3:35 PM