How to extract a range from a collection in powershell?

I have a large set of elements, and I want to extract elements from it. I need to skip some elements from the beginning, and some from the end.

The following example is simplified.

At first I tried to extract the bounding elements:

> ("a", "b", "c", "d", "e")[1,-2]
b
d  

This works as expected.

However, when I tried to extract the entire range, it returns something else than I want (unlike Python ['a', 'b', 'c', 'd', 'e'][1:-1], which works well).

> ("a", "b", "c", "d", "e")[1..-2]
b
a
e
d  

He turns the other way. How to change the direction of the loop?

I want to get: b c d.

Is there a solution without using the real length of the collection?

+5
source share
3 answers

1..- 2 . , .

$a = "a", "b", "c", "d", "e"
$a[1..($a.length-2)]
+5

, , , , , .

:

$a = "a", "b", "c", "d", "e"
($fromTop,$fromBottom) = 1, 2

...

$a | select -skip $fromTop | select -skip $fromBottom -last 1000000

... , PSCX, :

$a | skip -first $fromTop -last $fromBottom

b c length. , .

+3

To just remove the first two, you can do this:

$null,$null,$a = "a", "b", "c", "d", "e"
+1
source

All Articles