Arrays are collections of items accessed by index, and sometimes, you may need to manipulate these collections by removing elements from either the beginning or the end of the array. In this PowerShell tutorial, we’ll explore how to remove the first and last item in an array in PowerShell.
Removing the First Item from an Array in PowerShell
Removing the first item from an array, often referred to as ‘shifting’ an array, can be done in a few ways in PowerShell.
Method 1: Using Array Slicing
PowerShell supports array slicing, which allows you to create a new array by selecting a subset of the original array:
$array = 1, 2, 3, 4, 5
# Remove the first item
$array = $array[1..($array.Length - 1)]
In this example, [1..($array.Length - 1)]
creates a new array starting from the second element (index 1) to the last element.
Method 2: Using the ArrayList Class
Another way to remove the first item from an array in PowerShell is to use the System.Collections.ArrayList
class, which provides a RemoveAt
method:
$array = 1, 2, 3, 4, 5
# Convert array to ArrayList
$arrayList = [System.Collections.ArrayList]$array
# Remove the first item
$arrayList.RemoveAt(0)
# Convert back to array if needed
$array = $arrayList.ToArray()
$array
The RemoveAt(0)
method removes the first element of the ArrayList
(at index 0) in PowerShell.
After I executed the PowerShell script using VS code, you can see the output in the screenshot below.
Removing the Last Item from an Array in PowerShell
Similarly, you may want to remove the last item from an array in PowerShell, often referred to as ‘popping’ an array.
Method 1: Using Array Slicing
Just as we did with the first item, we can use array slicing to remove the last item from the PowerShell array:
$array = 1, 2, 3, 4, 5
# Remove the last item
$array = $array[0..($array.Length - 2)]
$array
Here, [0..($array.Length - 2)]
creates a new array that includes all elements except the last one.
You can see the output, after I executed the PowerShell script using VS code.
Method 2: Using the ArrayList Class
Using the ArrayList
class, we can remove the last item from the PowerShell array by calling RemoveAt
with the index of the last item:
$array = 1, 2, 3, 4, 5
$arrayList = [System.Collections.ArrayList]$array
# Remove the last item
$arrayList.RemoveAt($arrayList.Count - 1)
$array = $arrayList.ToArray()
$array
$arrayList.Count - 1
gives us the index of the last item, which is removed with RemoveAt
.
Remove Both the First and Last Item from the PowerShell Array
You can use the array slicing method to remove both the first and last elements from a PowerShell array.
Array slicing allows you to select a continuous subset of an array. To remove both the first and last items from the PowerShell array, you would slice the array from the second element to the second-to-last element:
$array = 1, 2, 3, 4, 5
# Remove the first and last items
$array = $array[1..($array.Length - 2)]
$array
In this example, 1
is the index of the second element (since arrays are zero-indexed), and $array.Length - 2
calculates the index of the second-to-last element. The new array $array
now contains the elements 2, 3, 4
.
You can see the output in the screenshot below:
This method is simple and effective for arrays that trim both ends in one operation. Remember that this creates a new array, and the original array remains unchanged unless you overwrite it with the new one.
Conclusion
In this PowerShell tutorial, I have explained how to remove the first and last element from an array in PowerShell using various methods.
- How to remove the first element from the PowerShell array?
- How to remove the last element from a PowerShell array?
- How to remove both the first and last element from an array in PowerShell?
You may also like:
- How to Get Unique Values from an Array in PowerShell?
- How to Remove Empty Lines from an Array in PowerShell?
- How To Split Comma Separated String To Array In PowerShell?
I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com