title: Get PowerShell object property from path in parameter
body: <p>How can I configure PowerShell to select an object property (top level or nested) based on the contents of a variable?</p>
  
  <p>I have PowerShell script that reads the contents of a file (JSON), converts it to an object and then selects one of the properties. However, the content is not consistent and the location of the desired property within the JSON may change.</p>
  
  <p>I'd like to be able to set a <code>-PropertyPath</code> parameter on the script that will enable users to pass in the path to the desired property within the object.</p>
  
  <p>Take the following example, which allows for the selection of an object property based on the value of <code>-PropertyPath</code>. It works because the property is not nested.</p>
  
  <pre><code>$PropertyPath= "myProperty"
  $definition = (Get-Content -Path $definitionFilePath -Raw | ConvertFrom-Json).$PropertyPath
  </code></pre>
  
  <p>Now take the following failed example where the property to get is nested (and does exist), which fails with no error, but <code>$definition</code> is empty - presumably because a property called "random.myProperty" does not exist.</p>
  
  <pre><code>$PropertyPath= "random.myProperty"
  $definition = (Get-Content -Path $definitionFilePath -Raw | ConvertFrom-Json).$PropertyPath
  </code></pre>
  
  <p>Any help would be appreciated.</p>