title: Using a Variable as a Property in PowerShell
body: <p>A bit of a PowerShell neophyte here, so please be kind. I am creating a PS script which is helping me to document my environment. What I am trying to do is get the names of all of an object's properties and populate an XML document with the property names and corresponding values. For example:</p>
  
  <pre><code>PS C:\&gt; $stuff = Get-SomeStuff
  PS C:\&gt; $stuff
  Name: foo
  Version: 1.0
  </code></pre>
  
  <p>The output XML would look like this:</p>
  
  <pre><code>&lt;rootNode&gt;
      &lt;stuff&gt;
          &lt;Name&gt;foo&lt;/Name&gt;
          &lt;Version&gt;1.0&lt;/Version&gt;
      &lt;/stuff&gt;
  &lt;/rootNode&gt;
  </code></pre>
  
  <p>Essentially I want to use the $stuff property names to substitute for the properties when retrieving the object properties. Essentially I need to dynamically substitute the property name with the extracted name. My code looks like this:</p>
  
  <pre><code>$stuff = Get-SomeStuff
  CreateXML $rootnode,"stuff",$stuff
  
  Function CreateXML ($rootNode, $nodeName, $category){
      $subRootNode = $xml.CreateElement($nodeName)
      $catProps = $stuff | Get-Member -MemberType Property | Select -ExpandProperty Name
      foreach ($prop in $catProps){
          $newNode = $xml.CreateElement($prop)
          $newNode.Text = $category.{$prop} #--- THIS IS THE LINE CAUSING PROBLEMS
          $subRootNode.AppendChild($newNode)
      }
  $rootNode.AppendChild($subRootNode)
  }
  </code></pre>
  
  <p>Any help is greatly appreciated, as there are thousands of properties I have to capture across multiple nodes.</p>