Devide 1000 users to 3 groups

This topic contains 4 replies, has 4 voices, and was last updated by  Fredrik Kacsmarck 5 months ago.

  • Author
    Posts
  • #71017

    turbo mcp
    Participant

    Hi there
    i am wondering what are my options in terms of away to devide 1000 users to 3 groups of users that i need to create diffrent tasks for.
    all 1000 users are in a specific ou in active directory.
    i know i can probably just query and export them to txt manually devide them to 3 txt files and then import each and do the task for that group.
    seems kind of "manual" to me
    i was wondering if someone has any other ideas ?

    Thanks in advance

  • #71020

    Richard Siddaway
    Moderator

    It depends on whether you want to divide the users randomly, take 1-333, 334-666, 667-1000 or there is some criteria you're going to use to split up the users

    • #71039

      turbo mcp
      Participant

      Thanks for your reply
      i just need to divide them to 3 ,basically to divide the load on the servers im running the task on.
      ill drop each "job/batch" on a different server
      that's my intention
      Thanks

  • #71071

    Kyle Hanson
    Participant

    This will work. May not be the best code.

    Will group users based on the last number in their SID. Won't be perfectly balanced, but will work for your use case.

    Get-ADUser -filter * | Group-Object -Property {$_.SID.Value.Substring(43,1) -le 3},{$_.SID.Value.Substring(43,1) -gt 3 -and $_.SID.Value.Substring(43,1) -le 6},{$_.SID.Value.Substring(43,1) -ge 7}

  • #71105

    Fredrik Kacsmarck
    Participant

    If you're looking for a "Round Robin" solution then you could use a counter.
    E.g.

    $users = # you need to populate this with e.g. Get-ADUser -Filter * -SearchBase "The DN of the OU"
    
    $counter = 1
    
    foreach($u in $users)
    {
     Switch($counter)
     {
      1 { Add-ADGroupMember -Identity "The First Group" -Members $u.SamAccountName; $counter++; break;}
      2 { Add-ADGroupMember -Identity "The Second Group" -Members $u.SamAccountName; $counter++; break;}
      3 { Add-ADGroupMember -Identity "The Third Group" -Members $u.SamAccountName; $counter = 1; break;}
     }
    }
    

    Just replace Add-ADGroupMember with whatever you want to do with them.
    Just used that as an example.

You must be logged in to reply to this topic.