but if I do
$thing = $pair.member
get-adobject -filter ‘name -like $thing’
it works.
I vaguely remember there’s some trick to using variable members inside of quotes. very vaguely. maybe there were parentheses involved. how do I get $pair.member directly into that filter without putting it into another variable?
“.” Is not a valid character for a variable, the parser resumes the string when it see “.member”.
What Naw suggested is correct. “$($object.property) rest of string” will force PowerShell to evaluate the expression in parentheses first and then place the value into the string.
And not just the Ad cmdlets. I’ve have run into this is others as well.
It must be remembered, at certain points in your code in your scripts, quoting a dotted variable, merely turns it in to a plain string.
This is one of the reasons I live in the PowerShell_ISE vs the PowerSHell console, because of the color coding, it is real obvious when stull like this will fail.
If you take any of the code segments shared thus far and paste them in the ISE, you will immediately see the difference.
This {$_.name -like “$pair.member”} is a string as noted by the darkred color of the dotted property.
This “(name = “$($pair.member)”)” is a concatenated string using a separate dotted variable, as noted by the black color of the dotted variable.
We all know the since quotes are string stuff, and is you really do not need variable expansion, don’t use double quotes.
If you need variable expansion, then of course that is what double quotes are for, with the exception of many filter use cases, like this one, where you have to do termination.