Ken
Ken

Reputation: 2838

Powershell How to Get list of computers from two different OU's in the same script

I know how to get a list of computers in specific OU's using powershell, and I can use a like command to; however I am looking . How can I get a list of computers in two different OU's that start Like COMP using Powershell ?

Upvotes: 0

Views: 1113

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174900

You could loop over the list of OUs and repeat the same query multiple times:

# fetch the target OUs
$OUs = Get-ADOrganizationalUnit -Filter "Name -like 'Comp*'"

# call Get-ADComputer once for each OU
$Computers = $OUs |ForEach-Object {
    Get-ADComputer -Filter * -SearchBase $_.distinguishedName
}

Upvotes: 2

Related Questions