finding index of key in an ordered dictionary in powershell
<p>I am having a little bit of trouble with hashtables/dictionaries in powershell.  The most recent roadblock is the ability to find the index of a key in an ordered dictionary.</p>

<ul>
<li><strong>I am looking for a solution that isn't simply iterating through the object.</strong>
(<em>I already know how to do that</em>)</li>
</ul>

<p>Consider the following example:</p>

<pre><code>$dictionary = [Ordered]@{
    'a' = 'blue';
    'b'='green';
    'c'='red'
    }
</code></pre>

<p>If this were a normal array I'd be able to look up the index of an entry by using IndexOf().</p>

<pre><code>[array]::IndexOf($dictionary,'c'). 
</code></pre>

<p>That would return 2 under normal circumstances.</p>

<p>If I try that with an ordered dictionary, though, I get -1.</p>

<p>Any solutions?</p>

<hr>

<p><strong><em>Edit:</em></strong>
In case anyone reading over this is wondering what I'm talking about.  What I was trying to use this for was to create an object to normalize property entries in a way that also has a numerical order.</p>

<p>I was trying to use this for the status of a process, for example:</p>

<pre><code>$_processState = [Ordered]@{
     'error' = 'error'
     'none' = 'none'
     'started' = 'started'
     'paused' = 'paused'
     'cleanup' = 'cleanup'
     'complete' = 'complete'
}
</code></pre>

<p>If you were able to easily do this, the above object would give <code>$_processState.error</code> an index value of 0 and ascend through each entry, finally giving <code>$_processState.complete</code> an index value of 5.  Then if you compared two properties, by "index value", you could see which one is further along by simple operators.  For instance:</p>

<pre><code>$thisObject.Status = $_processState.complete
If ($thisObject.Status -ge $_processState.cleanup) {Write-Host 'All done!'}

PS &gt; All done!
</code></pre>

<p>^^that doesn't work as is, but that's the idea.  It's what I was aiming for.  Or maybe to find something like $_processState.complete.IndexNumber()</p>

<p>Having an object like this also lets you assign values by the index name, itself, while standardizing the options...</p>

<pre><code>$thisObject.Status = $_processState.paused
$thisObject.Status

PS &gt; paused
</code></pre>

<p>Not really sure this was the best approach at the time or if it still is the best approach with all the custom class options there are available in PS v5.</p>