I have a file in a powershell script containing data, say:
1
2
3
If I get a random item from that list, and I have the full list, how could I get the index of the item (3 = $List[2]; 1 = $List[0]? The list is not actual numbers, but that was an example. I am using Powershell 5.1 and have no urgent need for speed, as the list is small.
>Solution :
Use IndexOf
Searches for the specified object and returns the index of its first
occurrence in a one-dimensional array or in a range of elements in the
array.
$List = 1,2,3,4,5
$Random = $List | Get-Random
$Random
5
$List.IndexOf($Random)
4
For further explanation check out this link