How to Remove the Last Element from an Array in PowerShell?

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
$array

In 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.

Remove the Last Element from an Array in PowerShell

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
$arrayList

In 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
$array

The 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:

PowerShell Remove the Last Element from an Array

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: