Get index of current item in a PowerShell loop

前端 未结 5 2022
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 00:19

Given a list of items in PowerShell, how do I find the index of the current item from within a loop?

For example:

$letters = { \'A\', \'B\', \'C\' }
         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 00:41

    For those coming here from Google like I did, later versions of Powershell have a $foreach automatic variable. You can find the "current" object with $foreach.Current

提交回复
热议问题