Quoting an Computer Object variable gives a different result then not quoting it

I’m trying to understand why I get different results for this.

$computer = Get-ADComputer -identity ComputerName

$computer.Name gives me the computer name only.

“$computer.Name” gives me the full Distinguished Name instead.

Why is that?

Was trying to use it in the following expression which doesn’t work.
$computer.servicePrincipalName.Contains("MSServerCluster/$computer.Name)

This works
$computer.servicePrincipalName.Contains(“MSServerCluster/”+$computer.Name)

But would like to understand why the different behaviour between quoted and unquoted.

Ivo,
Welcome to the forum. :wave:t4:

PowerShell starts to expand variables inside double quotes from left to right. So it will expand $Computer first before it comes to the period. If you want to expand particular properties you have to use a subexpression like this:

$computer = Get-ADComputer -Identity 'ComputerName'
"This way you get what you want: $($computer.Name)"

Here you can read more about:

https://ss64.com/ps/syntax-operators.html

BTW: When you post code or console output or error messages please format it as code using the preformatted text button ( </> ). Simply place the cursor on an empty line, click the button and paste your code.

Thanks in advance.

3 Likes

Thank you for the explanation. I had not realized that the eval would stop at the “.”
I figured the object name + . + variable was one unit. The Microsoft doc is not clear on this. The ss64 illustration is much better.
Thanks.

1 Like