Powershell where-object -notlike

powershell

If I pipe to where-object { $_.someProperty -notlike "*someValue*" }, the -notlike seems to filter more than just matches, but also objects whose someProperty is empty.

where-object -notlike from TechNet:

Specifies the Not-Like operator, which gets objects when the property value does not match a value that includes wildcard characters.

Based upon the definition, why does -notlike not return objects whose someProperty is empty? Since, no match is found?

Just to clarify:
I am performing Get-ADComputer -Filter * -Property MemberOf,someotherstuff,someotherstuff | where-object { $_.memberof -notlike "*somepartialdn*" }

The expectation is to have the computer object returned, since there is no match, even if a property value is null.

Best Answer

That depends on your definition of "empty".

If someProperty doesn't have a value, it's effective value is $null. Your string comparison doesn't apply to $null.

If someProperty is an empty string ("" or [String]::Empty) instead, the string comparison applies.

$values = "indonesia","turkmenistan",$null,"columbia"
$values |Where-Object {$_ -notlike "*istan"}
# Results in @("indonesia","columbia")

$values = "indonesia","turkmenistan",[String]::Empty,"columbia"
$values |Where-Object {$_ -notlike "*istan"}
# Results in @("indonesia","","columbia")