title: How to add properties to a PowerShell object from an array
body: <p>I have a two-dimensional array of property names and values, which I need to add to a PowerShell object.</p>
  
  <p>I have NO issues creating and displaying an object like this using <a href="http://technet.microsoft.com/en-us/library/hh849885.aspx" rel="noreferrer">New-Object</a> and <a href="https://technet.microsoft.com/en-us/library/hh849879.aspx" rel="noreferrer">Add-Member</a>:</p>
  
  <pre><code>$obj = New-Object PSObject
  $obj | Add-Member NoteProperty IName($fiel.IName)
  $obj | Add-Member NoteProperty SName($fiel.SName)
  $obj | Add-Member NoteProperty Taggy($fiel.Taggy)
  $obj | Add-Member NoteProperty Title($fiel.Title)
  
  Write-Output $obj
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/PmWJk.png" alt="Enter image description here"></p>
  
  <p>But when I try something like this:</p>
  
  <pre><code>for ($k=0; $k -lt $fieldsArray.Count; $k++)
  {
      $itemobj | Add-Member –MemberType NoteProperty –Name $fieldsArray[$k].InternalName –Value $itemki[$j][$fieldsArray[$k].InternalName]
      #Write-Host $k
      #Write-Host $fieldsArray[$k].InternalName.ToString()
      #Write-Host $itemki[$j][$fieldsArray[$k].InternalName]
  }
  
  Write-Output $itemobj
  </code></pre>
  
  <p>The Write-Output $itemobj will return only one property member that should be added without any neat column names. <br/>
  The commented out parts were added for testing purposes and return correct values for all items.</p>
  
  <p>I also tried</p>
  
  <pre><code>$itemobj | Add-Member NoteProperty $fieldsArray[$k].InternalName.ToString()($fieldsArray[$k].InternalName)
  </code></pre>
  
  <p>without any improvement.</p>
  
  <p>Why are the other property members not added?</p>
  
  <p>I have the data I need. If I write:</p>
  
  <pre><code>for ($k=0; $k -lt $fieldsArray.Count; $k++)
  {
      Write-Host $k
      Write-Host $fieldsArray[$k].InternalName.ToString()
      Write-Host $itemki[$j][$fieldsArray[$k].InternalName]
  }
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p><strong>0</strong> ID 1 <br/>
    <strong>1</strong> ContentTypeId 0x0108007345CD807822EA4E85691E5C642F3A27 <br/>
    <strong>2</strong> ContentType <br/>
    <strong>3</strong> Title Task0 <br/>
    <strong>4</strong> Modified 11/24/2014 12:29:30 PM</p>
  </blockquote>
  
  <p>And these are exactly the values that I expect and want. The problem is adding them as properties to an object. I think I cannot have a variable as a <code>NotePropertyName</code>, but it's a wild guess based on the results I am getting.</p>
  
  <p>Some of the values in $itemki[$j][$fieldsArray[$k].InternalName] are empty - could it be it?</p>
  
  <p>Forget all the arrays. They were just for the context:</p>
  
  <pre><code>Write-Host $fieldsArray[$k].InternalName.ToString() # Writes out the correct value
  Write-Host $itemki[$j][$fieldsArray[$k].InternalName] # writes out the correct value
  $itemobj | Add-Member NoteProperty $fieldsArray[$k].InternalName.ToString()($fieldsArray[$k].InternalName) # The values/property are not added
  </code></pre>
  
  <p>The question is: WHY NOT? Are there any restrictions in <code>Add-Member</code> on passing values as variables? Empty values?</p>