Filtering users by multiple values


I want do following. Filter users by City, Phone etc. Found with ones have wrong city or Phone put in AD.

I can do it without any problem with following commands run seperately:

$office | % {get-aduser -searchbase $ou -filter {(Enabled -eq $True) -and (PasswordNeverExpires -eq $False) -and (Office -notlike $)}}
$Company | % {get-aduser -searchbase $ou -filter {(Enabled -eq $True) -and (PasswordNeverExpires -eq $False) -and (Company -notlike $
$fax | % {get-aduser -searchbase $ou -filter {(Enabled -eq $True) -and (PasswordNeverExpires -eq $False) -and (Fax -notlike $_)}}

But now what i would want do is to run this 3 “together” and at the end export result to csv.

To put simple i want:

If user office is not set to New york AND Company not set to Apple AND Fax not set to 0000001 THEN export result to CSV

What will be the simpliest way - guess some simple script will be better then single line?

Well… you could certainly combine all of that into a single -Filter criteria. Have you tried that? But yes, it’d probably involve some nested ForEach loops or something. I don’t quite understand your input values so it’s a little hard to imagine the structure you’d need.

Not quite sure I follow the “together” part. Do you want a single list of users who have all 3 fields set wrong or a single list where any 1 of the criteria is met? In other words, -and or -or the 3 conditions?

use the -or comparison operator.

$splat = @{

filter={(Enabled -eq $True) -and (PasswordNeverExpires -eq $False)}
properties = 'office','fax','company'
searchbase = 'OU=...'


get-aduser @splat | ? {($ -ne 'home') -or ($_.fax -ne '555-5555') -or ($ -ne 'MSFT')}


$users = get-aduser @splat | %{


account = $_.samaccountname
office = $ -eq 'Home' 
fax = $_.fax -eq '555-5555' 
company = $ -eq 'MSFT'



$users | ? {($_|gm) -match 'False'}

This is cool but i dont think it will work the way i need :slight_smile:

I tried replacing -or with -and but altough it doesnt give me any error - it also doesnt show me user that i know have all this three parameters correct.

So just to clear up - yes i need to have this three parameters match. Option to include more than one office, more than one fax number would be cool also so i can use to find all account the are not setup correctly.

By multiple i mean:

Office like New York or Warsaw or Moscow
Fax Like 00001 or 00002 or 000003

If nothing of this match - list users

Those are just examples, you can adjust them at will to suit your needs. You can literally do anything with PS…Do you own a tesla?

Dan just wanted to say Kudos as I didn’t even think about making a Variable for a parameter for adusers.

What does your input file look like?

Do you mean the splatting? I like using it when the whole command wraps several lines in the shell. Easier to see whats going on.

Added bonus you can dive back into the hashtable if you didn’t want to return the default property set.

get-aduser @splat | select $