Extract a range of array in Powershell Extract a range of array in Powershell powershell powershell

Extract a range of array in Powershell


You can use the Range operator to slice the array:

$b = $a[1..2]


It is worth noting the Range operator supports dynamic values - very useful when you want to work out your range dynamically, for example:

$a = @(0,1,2,3,7)$b = @(4,5,6)$twotoseven = $a[($a.Length-($a.Length-2))..($a.Length-2)] + $b + $a[-1]

Output:

2 3 4 5 6 7