Powershell add-adgroupmember from variable with multiple values. RRS feed

  • Question

  • Hi,

    I'm working with the cmdlet Add-ADGroupMember.

    This command works fine in the following way:

    Add-ADGroupMember -Identity $Group -Members user1,user2,user3,user4,...................,user8000

    I'm able to add all 8000 users in less than 30 seconds with the above script.

    but when I'm trying to store all the users names with comma seperation in a variable and passing that variable to -Member argument, it is not working. I have a script which generates a text file with all 8000 users from a SQL Database. It generates the file in this format (usernames.txt)


    I'm loading all these comma separated values into a variable:

    $usernames = [IO.File]::ReadAllText("\\Share\directory\usernames.txt")
    #and then I'm passing this variable to the command below
    Add-ADGroupMember -Identity $Group -Members $usernames

    I'm getting the following error:

    Add-ADGroupMember : Cannot find an object with identity: 'user1,user2,user3,.......,user8000'

    I know I can loop through all individual 8000 users in the list and pass them through Add-ADGroupMember, but the problem is this loop is taking 3-4 minutes to add all 8000 users. 

    If i directly pass 8000 users in the -Member argument then it is just completing in 30 seconds to add all 8000 users. Can I make that variable passing to work? 

    Thanks & Regards,

    Monday, September 16, 2019 5:24 PM


All replies