Access variable attributes using Write-Output in PowerShell

Find out how to access attributes of variables using Write-Output in PowerShell.

John Savill

July 6, 2016

1 Min Read
Access variable attributes using Write-Output in PowerShell

Q. How do I access an attribute of a variable in PowerShell using Write-Output?

A. If you have an object that you want to output a parameter from it using Write-Output it does not behave as you would expect. For example:

Write-Output "Name of VM is $VM.VMName"

This would actually output the VM object then .VMName as text. What is actually required is a subexpression and this is used as follows:

Write-Output "Name of VM is $($VM.VMName)"

Note the subexpression is performed using $().

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like