select second or third object / element

前端 未结 4 1939
我寻月下人不归
我寻月下人不归 2021-01-02 05:51

I want to select the second/third/forth object of a Get-ChildItem statement in my PowerShell script. This gives me the first:

$first = Get-Child         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 06:16

    The second approach suggested by @AnsgarWiechers can easily be turned into a simple reusable funtion, like so:

    function Select-Nth {
        param([int]$N) 
    
        $Input | Select-Object -First $N | Select-Object -Last 1
    }
    

    And then

    PS C:\> 1,2,3,4,5 |Select-Nth 3
    3
    

提交回复
热议问题