powershell - select second or third object / element -
i want select second/third/forth object of get-childitem
statement in powershell script. gives me first:
$first = get-childitem -path $dir | sort-object creationtime -descending | select-object -first 1
this gives me first three:
$latest = get-childitem -path $dir | sort-object creationtime -descending | select-object -first 3
i second, or third, or fourth. (not first 2 , on).
is there way?
for selecting n-th element skip on first n-1 elements:
$third = get-childitem -path $dir | sort-object creationtime -descending | select-object -skip 2 | select-object -first 1
or select first n , of last element:
$third = get-childitem -path $dir | sort-object creationtime -descending | select-object -first 3 | select-object -last 1
beware, though, 2 approaches yield different results if input has less n elements. first approach return $null
in scenario, whereas second approach return last available element. depending on requirements may need choose 1 or other.
Comments
Post a Comment