Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way of getting the index of an item in a random list in powershell

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading