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.

This works

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

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:


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.


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.

1 Like