How to store an array of hashtables in a text file and then call all of the values for a given key in each hashtable
<p>I am using a text file as the backend for an application that I am developing. I first started off leaving the text file in a human-readable format but I decided that there was no sense in that figured it would be best to leave out formatting.</p>

<p>Where I am now in the backend dev process is creating a single-line hashtable with identical keys but different values for each entry. Seems logical and easy to work with.</p>

<p>Here is a mock-up of the entries in the text file:</p>

<pre><code>@{'bName'='1xx'; 'bTotal'='1yy'; 'bSet'='1zz'}
@{'bName'='2xx'; 'bTotal'='2yy'; 'bSet'='2zz'}
@{'bName'='3xx'; 'bTotal'='3yy'; 'bSet'='3zz'}
</code></pre>

<p>As you can see, the keys for each entry are identical, however, the values are going to be different. (<em>The numerical and repetitious nature of the values are purely coincidental and put in place for the sake of a mock-up. Actual values will not be numerically-oriented and won't be repetitious as seen in the example.</em>)</p>

<p>I am able to access keys and values by typing:</p>

<pre><code>$hash = Get-Content .\Desktop\Test.txt | Out-String | iex
</code></pre>

<p>which outputs:</p>

<pre>Name                    Value
----                    -----
bName                   1xx
bTotal                  1yy
bSet                    1zz
bName                   2xx
bTotal                  2yy
bSet                    2zz
bName                   3xx
bTotal                  3yy
bSet                    3zz</pre>

<p>What I ultimately want to do is gather each of the values for <code>bName</code>, <code>bTotal</code>, and <code>bSet</code> so that I can append each to a separate WinForms ComboBox. The WinForms part will be simple, I am just having a bit of an issue with getting the values from each hashtable in the text file.</p>

<p>I tried:</p>

<pre><code>$hash.Values | ?{$hash.Keys -contains 'bName'}
</code></pre>

<p>but it just prints out every <code>$hash.Value</code> regardless of the <code>$hash.Key</code> match given in the pipe.</p>

<p>I understand that <code>$hash</code> is an array and I figured I may have to pipe out each iteration in a <code>foreach ($hash | %{})</code> loop but I'm not quite sure the correct way to do this. For example, when I try:</p>

<pre><code>$hash | $_.Keys
</code></pre>

<p>or</p>

<pre><code>$hash | $_.Values
</code></pre>

<p>it isn't treating each iteration like a hashtable.</p>

<p>What am I doing wrong here? Am I going about it in a convoluted way while there is a much easier way to accomplish this? I am open to all sorts of ideas or suggestions.</p>

<hr>

<p><strong>As an afterthought</strong>: It is kind of funny how often an obvious solution presents itself when you step away and divert your attention towards something else.</p>

<p>I went to grab lunch and I can't, for the life of me, begin to comprehend why I didn't realize that I could just very easily do this:</p>

<pre><code>$hash.bName
</code></pre>

<p>or:</p>

<pre><code>$hash.bTotal
</code></pre>

<p>or:</p>

<pre><code>$hash.bSet
</code></pre>

<p>That will do exact as I was wanting to do. However, considering the answers provided, I may go a different route in terms of using an .ini file in CSV format rather than creating an array of hashtables.</p>