0

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?

closed as off-topic by mustaccio, Randi Vertongen, Michael Green, LowlyDBA, Gaius Mar 8 at 14:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3

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)"
  • Thanks for the help. So is doing the following ... Write-Output " This $testValue will print just fine." ... incorrect? This syntax still works for simple variables for some reason. – Fractal Mar 6 at 20:48
  • 1
    Basic data type (int, string, char, float etc) variables do not require above special syntax. Above syntax is only required for object property values. – Ajay Dwivedi Mar 7 at 4:26
  • Got it. Okay thanks again. – Fractal Mar 10 at 19:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.