Often you may want to slice an array in PowerShell to extract only specific elements.
You can use the following methods to do so:
Method 1: Extract Specific Range of Elements from Array
$my_array[3..8]
This particular example will extract each of the elements in index positions 3 through 8 in the array named my_array.
Note: The first element in the array has an index value of 0.
Method 2: Extract First N Elements from Array
$my_array | Select -First 7
This particular example will extract the first 7 elements in the array named my_array.
Method 3: Extract Last N Elements from Array
$my_array | Select -Last 7
This particular example will extract the last 7 elements in the array named my_array.
The following examples show how to use each of these methods in practice.
Example 1: Extract Specific Range of Elements from Array in PowerShell
Suppose that we create the following array in PowerShell named $my_array:
$my_array = 4, 15, 20, 22, 30, 45, 50, 51, 23, 29
We can use the following syntax to select only the elements in index positions 3 through 8 in the array:
$my_array[3..8]
The following screenshot shows how to use this syntax in practice:

Notice that this returns only the elements in index positions 3 through 8 from the array.
Example 2: Extract First N Elements from Array in PowerShell
Once again suppose that we create the following array in PowerShell named $my_array:
$my_array = 4, 15, 20, 22, 30, 45, 50, 51, 23, 29
We can use the following syntax to select only the first 7 elements from the array:
$my_array | Select -First 7
The following screenshot shows how to use this syntax in practice:

Notice that this returns only the first 7 elements from the array.
Example 3: Extract Last N Elements from Array in PowerShell
Once again suppose that we create the following array in PowerShell named $my_array:
$my_array = 4, 15, 20, 22, 30, 45, 50, 51, 23, 29
We can use the following syntax to select only the last 7 elements from the array:
$my_array | Select -Last 7
The following screenshot shows how to use this syntax in practice:

Notice that this returns only the last 7 elements from the array.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Compare Two Arrays
PowerShell: How to Get First Item in Array
PowerShell: How to Calculate the Average of an Array
PowerShell: How to Find Duplicate Values in Array
