1

Is there a better way to filter for objects via where-object then to send the data through multiple pipelines?

$clients = Get-ADComputer 
                -SearchBase "OU=Clients,DC=contoso,DC=com" 
                -Filter * 
                -Properties Description,OperatingSystem

$clients | Where OperatingSystem -notlike "*Windows 7*" 
                 | Where OperatingSystem -notlike "*Windows 10*"  

Ideal would be a complex filtering mechanism like we can use for the -Filter Parameter. I would have expected to be able to use something like the following...

$Clients | Where { 
    (OperatingSystem -notlike "Windows 7") -and (OperatingSystem -notlike "Windows 10")
}
2
  • 4
    (OperatingSystem -> ($_.OperatingSystem Commented Feb 14, 2018 at 14:18
  • As @PetSerAl pointed out your issue is accessing the property correctly. -AND works fine in a Where-Object filter. Commented Feb 14, 2018 at 15:28

3 Answers 3

3
$clients | Where OperatingSystem -notlike "*Windows 7*" |
    Where OperatingSystem -notlike "*Windows 10*"  

Strictly speaking, this should work.

However, the problem you're running in to is that the simplified Where-Object syntax shown above only works in the most simple cases. When you use the full syntax, you must specify the properties using the $_ variable:

$clients | Where-Object {
    ($_.OperatingSystem -notlike '*Windows 7*') -and ($_.OperatingSystem -notlike '*Windows 10*')
}

However, since you're using Get-ADComputer, you really should be using the -Filter property on that command. It will be much faster, and will be less work for your domain controller, too:

Get-ADComputer -SearchBase "OU=Clients,DC=contoso,DC=com" `
    -Filter "(OperatingSystem -notlike '*Windows 7*') -and (OperatingSystem -notlike '*Windows 10*')" `
    -Properties Description,OperatingSystem
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for replying. I am aware of the burden put on the network and the domain controller. But to my apology I have to add, that I was doing further processing on the data, requiring more then just "Windows 7" objects, ... so pulling "all" data (noting the -searchBase restriction) was the better approach.
1

It is more efficient to filter directly in the query rather than filtering after-the-fact using Where-Object (which retrieves all objects first). Example using the -LDAPFilter parameter:

Get-ADComputer -LDAPFilter "(&(!operatingSystem=Windows 7*)(!operatingSystem=Windows 10*))" -Properties operatingSystem,description

1 Comment

This comes up so many times it should be in a PowerShell FAQ. Active Directory is a database. You would never retrieve all columns on all tables from your SQL database then filter it in application memory. LDAP is no different.
-1

Something like this perhaps?

$DesktopClients=@('Windows 7','Windows 10')
$Clients=$Clients -notmatch ($DesktopClients -join '|')

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.