Forum Discussion

Robert Bollinger's avatar
Robert Bollinger
Iron Contributor
Jul 18, 2018

Script to select users from specific OUs and with Specific Department codes.

Hey Team, 

I would like to do the following: 

 

1) Search 5 separate OUs 

2) Select All users from those OUs with a specific Department code - DepartmnetCode looks like 123456 (4-7 numbers, all in a list).

3) Select only users with a City specific city defined in their ad user account (Los Angeles, Santa Monica) etc. 

 

I have about 120 department codes in a text file. some of our users have those department codes defined in their AD accounts. I can define the variables easy enough but the place i always hit the most difficulty is the foreach commands. 

 

$DepartmentCode = Get-Content c:\temp\departmentCode.txt

$ous = 'OU=Test 1,DC=domain,DC=com','OU=Test 2,DC=domain,DC=com'

$Cities = "Los Angeles", "Santa Monica", "Playa Vista", "Culver City"

 

I know that i can use SearchBase

 

Get-ADUser -SearchBase -Filter * -SearchBase $_ous? but how do i tell PowerShell to only select users in that have a matching City in their AD Account as per the $Cities Variable? and also how to i tell PS to only select users that have a departmentcode (defined on their ad user account, and in my department code list). ?

 

Any ideas? Maybe a similar script so i can see how it might work ? 

 

Thanks, 

 

Robert 

 

 

Resources