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

%3CLINGO-SUB%20id%3D%22lingo-sub-216853%22%20slang%3D%22en-US%22%3EScript%20to%20select%20users%20from%20specific%20OUs%20and%20with%20Specific%20Department%20codes.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-216853%22%20slang%3D%22en-US%22%3E%3CP%3EHey%20Team%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20do%20the%20following%3A%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1)%26nbsp%3BSearch%205%20separate%20OUs%26nbsp%3B%3C%2FP%3E%3CP%3E2)%20Select%20All%20users%20from%20those%20OUs%20with%20a%20specific%20Department%20code%20-%20DepartmnetCode%20looks%20like%20123456%20(4-7%20numbers%2C%20all%20in%20a%20list).%3C%2FP%3E%3CP%3E3)%20Select%20only%20users%20with%20a%20City%20specific%20city%20defined%20in%20their%20ad%20user%20account%20(Los%20Angeles%2C%20Santa%20Monica)%20etc.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20about%20120%20department%20codes%20in%20a%20text%20file.%20some%20of%20our%20users%20have%20those%20department%20codes%20defined%20in%20their%20AD%20accounts.%20I%20can%20define%20the%20variables%20easy%20enough%20but%20the%20place%20i%20always%20hit%20the%20most%20difficulty%20is%20the%20foreach%20commands.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24DepartmentCode%20%3D%20Get-Content%20c%3A%5Ctemp%5CdepartmentCode.txt%3C%2FP%3E%3CP%3E%24ous%20%3D%20'OU%3DTest%201%2CDC%3Ddomain%2CDC%3Dcom'%2C'OU%3DTest%202%2CDC%3Ddomain%2CDC%3Dcom'%3C%2FP%3E%3CP%3E%24Cities%20%3D%20%22Los%20Angeles%22%2C%20%22Santa%20Monica%22%2C%20%22Playa%20Vista%22%2C%20%22Culver%20City%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20know%20that%20i%20can%20use%20SearchBase%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-ADUser%20-SearchBase%20-Filter%20*%20-SearchBase%20%24_ous%3F%20but%20how%20do%20i%20tell%20PowerShell%20to%20only%20select%20users%20in%20that%20have%20a%20matching%20City%20in%20their%20AD%20Account%20as%20per%20the%20%24Cities%20Variable%3F%20and%20also%20how%20to%20i%20tell%20PS%20to%20only%20select%20users%20that%20have%20a%20departmentcode%20(defined%20on%20their%20ad%20user%20account%2C%20and%20in%20my%20department%20code%20list).%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%3F%20Maybe%20a%20similar%20script%20so%20i%20can%20see%20how%20it%20might%20work%20%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERobert%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-216853%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-217740%22%20slang%3D%22en-US%22%3ERe%3A%20Script%20to%20select%20users%20from%20specific%20OUs%20and%20with%20Specific%20Department%20codes.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-217740%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20you%20need%20to%20look%20at%20is%20Where-Object.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fwhere-object%3Fview%3Dpowershell-6%26nbsp%3B%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fwhere-object%3Fview%3Dpowershell-6%26nbsp%3B%3C%2FA%3E%3C%2FP%3E%3CP%3EYou%20could%20do%20something%20like%20this%3A%3C%2FP%3E%3CP%3E%3CSPAN%3EGet-ADUser%20-SearchBase%20-Filter%20*%20-SearchBase%20%24ous%20%7C%20Where-Object%26nbsp%3B%20%7B%24departmentcode%20-contains%20%24_.department%20-and%20%24Cities%26nbsp%3B%20-contains%20%24_.City%7D%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Contributor

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 

 

 

1 Reply
Highlighted

What you need to look at is Where-Object.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=power...

You could do something like this:

Get-ADUser -SearchBase -Filter * -SearchBase $ous | Where-Object  {$departmentcode -contains $_.department -and $Cities  -contains $_.City}