Add users from multiple OUs to an AD group

Hello EE

Can someone help me with this ?

I need to add all the users form the top level OU called Los Angeles
to a group called LosAngelesAll( User objects that are only seven digits long )

So the script needs to check City 1/Users , City 2/Users and so forth ..
Los Angeles OU
--> City 1
      --> Users
--> City 2
      --> Users
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
you want the users in city1/city2 with a constraint of the username length being 7 characters to be added to LosAngelesAll ??
MilesLoganAuthor Commented:
Hi David , yes that is correct .
Darshana JayathilakeSystem EngineerCommented:
you can use the below script for the task.Testing purpose i have used adgroup called help_desk

import-module activedirectory
$tempgroup=get-aduser -filter {city -eq "colombo"}
for($i=0; $i -le $intarrcount; $i++)

Add-ADGroupMember -Identity Help_desk -Members $tempgroup[$i]

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

One option is to add users to a group, another that achieves the same goal is have a user become a member of a group.

Using the example darshasam posted, you will loop through the get-aduser that filters on OU users, then compare the username length, and if it meets the criteria, add the group to the user, or add the user to the group.

Powershell reference
David Johnson, CD, MVPOwnerCommented:
$sb = @()
$sb += "City_1"
$sb += 'City_2'
 Get-ADGroupMember -Identity LosAngelesAll | select samaccountname
for($i=0;$i -lt $sb.count;$i++)
    $search = "OU=" + $sb[$i] + ',OU=Los Angeles,DC=deployment-guy,DC=local' 
    $users = Get-ADUser -filter * -searchbase $search 
    foreach ($user in $users) 
        write-host($user.SamAccountName +"     "+ $sb[$i])
        if ($user.samaccountname.length -eq 7)
            Add-ADGroupMember -identity LosAngelesAll -Members $user
 Get-ADGroupMember -Identity LosAngelesAll | select samaccountname 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MilesLoganAuthor Commented:
Hi David , thank you so much ..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.