Do you want to remove the last element from an array in PowerShell? In this PowerShell tutorial, I will explain various methods to remove the last element from an array in PowerShell.
To remove the last element from an array in PowerShell, use the array variable without the last item. For example, if $array is your array, $array = $array[0..($array.Length - 2)] will remove the last element. This command works by creating a new array that excludes the last item.
Remove the Last Element from an Array in PowerShell
Now, let us check out various methods to remove the last element from an array in PowerShell.
Using Array Slicing
One of the simplest methods to remove the last element from an array is to use array slicing in PowerShell. This method does not actually modify the original array; instead, it creates a new array that excludes the last element.
Here’s an example script:
# Define the original array
$array = 1, 2, 3, 4, 5
# Remove the last element by slicing the array
$array = $array[0..($array.Length - 2)]
# Display the modified array
$arrayIn this script, $array.Length - 2 calculates the index of the second-to-last element. The slicing [0..($array.Length - 2)] creates a new array that includes all elements from the first to the second-to-last, effectively removing the last element.
You can see the output of the PowerShell script after I executed the code using PowerShell ISE.

Using ArrayList
Another method is to use an ArrayList, which is a dynamic array that allows you to add and remove items easily. First, you need to create an ArrayList from your existing array and then use the RemoveAt() method to remove the last element in PowerShell.
Here’s how you can do it:
# Create an array
$array = 1, 2, 3, 4, 5
# Convert array to ArrayList
$arrayList = [System.Collections.ArrayList]$array
# Remove the last element
$arrayList.RemoveAt($arrayList.Count - 1)
# Display the modified ArrayList
$arrayListIn this script, $arrayList.Count - 1 finds the index of the last element, and RemoveAt() removes it from the ArrayList.
Using the Select-Object Cmdlet
PowerShell also offers cmdlets like Select-Object which can be used to select objects based on certain criteria. You can use it to select all but the last element of the PowerShell array.
Here is a complete example:
# Define the original array
$array = 1, 2, 3, 4, 5
# Use Select-Object to skip the last element
$array = $array | Select-Object -SkipLast 1
# Display the modified array
$arrayThe Select-Object -SkipLast 1 command skips the last element of the array and passes the rest to the pipeline, effectively removing the last element. You can see the output in the screenshot below:

Conclusion
Removing the last element from an array in PowerShell is easy and can be possible using different methods. However, by using array slicing, converting to an ArrayList, or utilizing the Select-Object cmdlet, you can easily manipulate arrays to suit your needs.
In this PowerShell tutorial, I have explained, how to remove the last element from an array in PowerShell using different methods with real examples.
You may also like:
- How to Get the First Element of an Array in PowerShell?
- How to Select the Last Item in an Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.