mjolinor's answer is conceptually elegant, but slow with large arrays, presumably due to having to build a parallel array of indices first (which is also memory-inefficient).
It is conceptually similar to the following LINQ-based solution (PSv3+), which is more memory-efficient and about twice as fast, but still slow:
$arr = 'A','D','B','D','C','E','D','F'
[Linq.Enumerable]::Where(
[Linq.Enumerable]::Range(0, $arr.Length),
[Func[int, bool]] { param($i) $arr[$i] -eq 'D' }
)
While any PowerShell looping solution is ultimately slow compared to a compiled language, the following alternative, while more verbose, is still much faster with large arrays:
PS C:\> & { param($arr, $val)
$i = 0
foreach ($el in $arr) { if ($el -eq $val) { $i } ++$i }
} ('A','D','B','D','C','E','D','F') 'D'
1
3
6
Note:
Perhaps surprisingly, this solution is even faster than Matt's solution, which calls [array]::IndexOf()
in a loop instead of enumerating all elements.
Use of a script block (invoked with call operator &
and arguments), while not strictly necessary, is used to prevent polluting the enclosing scope with helper variable $i
.
The foreach
statement is faster than the Foreach-Object
cmdlet (whose built-in aliases are %
and, confusingly, also foreach
).
Simply (implicitly) outputting $i
for each match makes PowerShell collect multiple results in an array.
- If only one index is found, you'll get a scalar
[int]
instance instead; wrap the whole command in @(...)
to ensure that you always get an array.
While $i
by itself outputs the value of $i
, ++$i
by design does NOT (though you could use (++$i)
to achieve that, if needed).
Unlike Array.IndexOf()
, PowerShell's -eq
operator is case-insensitive by default; for case-sensitivity, use -ceq
instead.
It's easy to turn the above into a (simple) function (note that the parameters are purposely untyped, for flexibility):
function get-IndicesOf($Array, $Value) {
$i = 0
foreach ($el in $Array) {
if ($el -eq $Value) { $i }
++$i
}
}
# Sample call
PS C:\> get-IndicesOf ('A','D','B','D','C','E','D','F') 'D'
1
3
6