Get-ADUser return a single account from all OU's and Sub OU's

652 views Asked by At

I have the following sample OU structure in my Active Directory server

MY-OU-STRUCTURE
I have user accounts in the "users" OU in each of OU1, OU2 and so on. The user accounts have a static prefix e.g. OU1 will have user accounts like OU1user1,OU1user2,OU1user3 and so on. Similarly OU2 and OU3 will have use accounts like OU2user2,OU2user2,OU2user3 & OU3user1,OU3user2,OU3user3

Now what I want?

I want only a single user (it can be any user) from all of the OU's under the RootOU. Currently i am using the following command and its returning all the users inside the RootOU's sub-OU.

$ou = "OU=RootOU,DC=mydomain,DC=com"
$myUsers = Get-ADUser -Filter *  -SearchBase $ou -SearchScope 2

1

There are 1 answers

1
Theo On BEST ANSWER

You can probably do something like

$myUsers = Get-ADOrganizationalUnit -Filter "Name -like '*users*'" -SearchBase $ou -SearchScope 2 | ForEach-Object {
    Get-ADUser -Filter * | Select-Object -First 1
}