How to Get options for -filter command in Get-adComputer



I know this is a noob question. I understand the -filter param on the “get-adcomputer” command is filtering most likely by using an object value(or ldap value); however I cant seem to find out how i would find out those values are (the possible options for filter param)


I could open the psm and look inside identifying the code; however I must be missing some basics here. :slight_smile:


If I assign get-adcomputer to a var and then output the var to select-object *; I don’t get a value that represents (as an example) “Get-AdComputer -filter {OperatingSystem -like “Windows Server”}”


Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*'}


Tried; (with no luck)

get-command -Name Get-adComputer | Select-Object *

get-member -inputobject Get-ADComputer -MemberType All


Thank you,


The filter value is a string in the form propertyName -operator propertyValue. However, how the string and values are quoted do matter. You should never use the script block notation { property -operator value }despite what the documentation shows because it is incorrect. The reason quoting gets wonky is because the local PowerShell shell can interpolate strings and the called Get-AdComputer can expand strings. Here are some acceptable examples:

# Using only strings
Get-ADComputer -Filter "OperatingSystem -like '*Windows Server*'"

# Using simple variable references
$Value = '*Windows Server*'
Get-ADComputer -Filter 'OperatingSystem -like $Value'

# Using variables with properties
$Value.OperatingSystem = '*Windows Server*'
Get-ADComputer -Filter "OperatingSystem -like '$($Value.OperatingSystem)'"

The outer quotes are handled by the local PowerShell parser. The inner value is passed to the command for further parsing. Any variable inside of a outer double quotes is expanded inside. So if you want the command to interpret the variable instead of the calling PowerShell parser, you must use single quotes. Basically, if you want the command to determine the variable value, then use outer single quotes and nothing inside. If you want the calling shell to interpret first, then use outer double quotes. But since the inside value will be expanded before being sent to the command, you must give the value inner single quotes so the command will know it is a string.

Due to how $ syntax is handled within strings, you must surround the reference in the sub-expression operator $() . Otherwise, only the variable $object gets interpolated and then .property is literally just appended to the end of the result. Since you want PowerShell to parse that sub-expression for you, you can use outer double quotes and inner single quotes.