AdminOfThings' helpful answer shows one approach to providing key-based access to values, a [pscustomobject]
.
In general terms, you're looking for a dictionary or hash table (hashtable): a collection of key-value pairs that allow efficient lookup of a value by its associated key.
In fact, the [pscustomobject]
technique in the linked answer is syntactically based on PowerShell's hashtable literal syntax, @{ <key> = <value>; ... }
[1].
Example using a script block ({ ... }
) instead of a script/function for brevity:
$output = & {
# Output a hashtable
@{ x = 'one'; y = 'two' }
}
# Access the entries by key:
# Index syntax:
$output['x'] # -> 'one'
$output['y'] # -> 'two'
# Dot notation, as with objects, works too.
# While this is convenient, it is much slower with variable-based keys;
# e.g., $output.$keyName - this typically won't matter, but may in loops
# with a high number of iterations.
$output.x # -> 'one'
$output.y # -> 'two'
If the order in which entries are enumerated matters, use an ordered hashtable (PSv3+): [ordered] @{ <key> = <value>; ... }
.
You only pay a negligible performance penalty for using an ordered hashtable, so I suggest using [ordered]
as a matter of habit, as it makes for a nicer output and debugging experience to see the entries in the same order in which they were defined.
Choosing between a [pscustomobject]
and a hashtable:
If you conceive of the output as a single object with the values stored in properties, use [pscustomobject]
, but note that constructing an object is slower than constructing a hashtable.
If the output is just a collection of key-value pairs, use a(n ordered) hashtable.
If performance matters (in loops with many iterations):
Use (ordered) hashtables.
Use index access with non-literal keys (e.g., $key = 'one'; $ht[$key]
), which is much faster than non-literal dot notation (e.g., $key = 'one'; $ht.$key
)
[1] Note, however, that [pscustomobject] @{ ... }
is syntactic sugar in that a custom object is directly constructed - no intermediate hashtable is created; additionally, the properties defined by the hashtable keys are defined in definition order, whereas a hashtable guarantees no particular ordering of its entries unless you use [ordered] @{ ... }
.