2

I am working with PowerShell commands. I want to filter records with one field name description. I successfully filter with one description named "school". My command is:

Get-ADuser -filter {(Description -eq "school")} -Properties * | select *

But I want to filter records with multiple values of description like "school", "college", etc. How will this be possible?

3 Answers 3

10

You could use an -or statement:

Get-ADuser -filter {(Description -eq "school") -or (Description -eq "college")} -Properties * | select *

Or you could create an array and filter the results, although this is filtering after the query executes, so it may take longer. It would make sense to try and apply a filter to Get-AdUser before passing it through where-object:

@filter = @("school", "college")
Get-ADuser -Properties * | where-object{@filter -contains $_.Description} | select *
Sign up to request clarification or add additional context in comments.

2 Comments

If your question has been answered, please mark the answer that you feel best addressed your question as accepted, rather than posting a comment thanking the answerer. Once you have at least 15 rep, you can also upvote the accepted answer and any other answers you found useful.
The second approach violates the principle of "filter left, format right" -- it brings back lots of records from AD, and then filters them locally. I like the answer from @TNT for that reason.
5

David Martin's approach is a bit static. If we want to search for a varying number of descriptions we need to do something like:

$descriptions = "school", "college"
$filter = ($descriptions | % { "Description -eq '$_'" }) -join ' -or '
Get-ADuser -filter $filter -Properties Description

Comments

3

Try this:

get-aduser -filter * -properties *|? {$_.description -like "school" -or $_.description -like "college"}

If you want to search for descriptions that contain school, add "*school*" and it'll look for any instance of school in the description.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.