0

In my Powershell script, I have an object that I can navigate and get MyProperty value by doing something like:

$x = $obj.Level1.Level2.MyProperty
write-host "MyProperty value is : $($x)"

But, if i change my code as the following, then I am no longer able to get MyProperty value any more:

$mypath= "Level1.Level2"
$x = $obj."$($mypath)".MyProperty
write-host "MyProperty value is : $($x)"

The bottom line is, I need to use variables to navigate the object. I am certain that I must be doing something wrong here, as I am new to Powershell scripting, so please advise - how do I use path in variables to navigate an object?

Alexu
  • 1,015
  • 2
  • 12
  • 32
  • 1
    Why do you think you need to use a variable to navigate your object? You may elaborate a little more detailed. There might be another/better way. ;-) – Olaf Mar 22 '23 at 21:48
  • I have found what I look for. For someone who is new to Powershell like me, here is the link https://stackoverflow.com/questions/39667296/how-to-access-a-property-from-an-object-using-a-variable-name – Alexu Mar 22 '23 at 22:12

0 Answers0