powershell Write-Output format I am finding what seems to be an inconsistency to the way a Write-Output line (which prints out a variable) should be formatted. I can't find any information on this, which makes me guess that I am misunderstanding what I am seeing.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Statement:

For most simple variables, the below will work:

Write-Output " This $testValue will print just fine."


However, I need to add a $() to the Write-Output line if the variable is part of an object:

Write-Output $("Removing old software from directory: " + $site.INSTALLDIR)


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Question: Is the above statement correct?
When you want to print object property values, then it should be used with special syntax $() shown as below:-

Write-Output "Removing old software from directory: $($site.INSTALLDIR)"


In case you want to print the variable name as well, put a backtick before the variable usage. Example:-

Write-Output "Removing old software from directory (`$site.INSTALLDIR): $($site.INSTALLDIR)"