PowerShell: Get an object's property value by a dynamic property name
The value of an object's property whose name is dynamic (for example stored in a variable) can be looked up with $obj.psObject.properties['name'].value:
$obj = new-object psObject -property @{
num = 42
txt = 'Hello world'
}
$key = 'num'
write-host "value of $key is $($obj.psObject.properties[$key].value)"
However, such a value can also be looked up by the much shorter syntax $obj.$key:
write-host "value of $key is $($obj.$key)"
If the dynamic property name to be looked up is calculated «in place», parantheses can be used: $obj.( expr ):