retrieve array index inside Powershell foreach loop?

Question:

I have a loop that uses the foreach keyword:

Is it possible to find out the array index referred to by $ln during the iteration? Or do I need to create and increment a separate counting variable for each loop iteration?

Answer:

Yes, either use a for loop (something like (gc c:\ATextFile.txt).count would be the upper limit ) or an external counter.

Related answer ( for C#, but basically both use the same enumerable concepts): How do you get the index of the current iteration of a foreach loop?

Source:

retrieve array index inside Powershell foreach loop? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply