I have an array of strings. Not sure if there is simple way to get the index of an item first found in the array?
# example array
$array = "A", "B", "C"
$item = "B"
# the following line gets null, any way to get its index?
$index = $array | where {$_ -eq $item} | ForEach-Object { $_.Index }
I could do it in a loop. not sure if there is any alternative way?
Use a for loop (or a foreach loop that iterates over the array index…same difference). I don’t know of any system variable that holds the current array index inside a foreach loop, and I don’t think one exists.