title: Is it possible to add properties to collections in powershell?
body: <p>I've got a very large array of objects obtained by a normal Get-ChildItem, and I need to be able to index them by a variety of different properties. I've got a set of hashtables that I've made that index them by those properties, but right now it's just a bunch of collections that must be built and managed separately. At one point it occurred to me that it would be nice to just add the hashtables onto the base collection as properties such as "ByPath" "ByGuid" etc. The code to build it would just draw together the individual statements I already have:</p>
  
  <pre><code>$items = Get-ChildItem -recurse blahblah
  $items | Add-Member -membertype NoteProperty -name "ByGuid" -value (Get-ItemsByGuid)
  $items | Add-Member -memberType NoteProperty -name "ByPath" -value (Get-ItemsByPath)
  $items
  </code></pre>
  
  <p>Unfortunately, while this doesn't throw on execution, it doesn't do anything. The properties exist and are visible with Get-Member, but they're always null when interrogated and throw when being set afterwards.</p>
  
  <p><code>Property 'ByGuid' cannot be found on this object; make sure it exists and is settable.
  At line:1 char:14</code>
  <code>+ $items. &lt;&lt;&lt;&lt; ByGuid = $itemsByGuid</code>
  <code>+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException</code>
  <code>+ FullyQualifiedErrorId : PropertyAssignmentException</code></p>
  
  <p>lies!</p>
  
  <p>Is there any reason that this shouldn't be possible based on what I'm trying to do? Or is there some reason for the oddly mixed messages I'm getting?</p>