I’m missing something here:
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter = ('(objectclass=computer)') $computers = $objSearcher.findall()
So the question is why do the two following outputs differ?
$computers | %{ 'Server name in quotes $_.properties.name' 'Server name not in quotes ' + $_.properties.name } PS> $computers[0] | %{'$_.properties.name'; $_.properties.name} System.DirectoryServices.SearchResult.properties.name GORILLA
When you included $_.properties.name in the string, it was returning the type name of the property. When a variable is included in a string and the string is evaluated, it calls the ToString method on that object referenced by the variable (not including the members specified after).
In this case, the ToString method is returning the type name. You can force the evaluation of the variable and members similar to what EBGreen suggested, but by using
In the other scenario PowerShell is evaluating the variable and members specified first and then adding it to the previous string.
You are right that you are getting back a collection of properties. If you pipe $computer[0].properties to get-member, you can explore the object model right from the command line.
The important part is below.