How to Remove Array Element by Index in PowerShell?

Recently, I got a requirement to remove an array element by index in PowerShell. In this PowerShell tutorial, I will explain different ways to remove array elements by index in PowerShell.

To remove an element by index in PowerShell, you can use the array slicing method to create a new array without the element. For example, $newArray = $array[0..($indexToRemove-1)] + $array[($indexToRemove+1)..($array.Length-1)] will remove the element at $indexToRemove. Alternatively, you can utilize an ArrayList and its RemoveAt method for a more direct approach: $arrayList.RemoveAt($indexToRemove). Both methods effectively remove an element from an array at a specified index.

Remove Array Element by Index in PowerShell

An array is a collection of items that can be accessed by a numerical index. In PowerShell, arrays are fixed in size, meaning once you define an array, you cannot increase or decrease its size directly.

Now, let us see different methods to remove array elements by index in PowerShell.

Method 1: Creating a New Array Without the Element

One way to remove an element from an array by an index is to create a new array that excludes the element at the specified index in PowerShell. Here’s an example:

# Original array
$array = 1, 2, 3, 4, 5

# Index to remove
$indexToRemove = 2

# Create a new array excluding the index
$newArray = $array[0..($indexToRemove-1)] + $array[($indexToRemove+1)..($array.Length-1)]

# Display the new array
$newArray

This method uses array slicing to create two sub-arrays: one before and one after the index to remove. These are then concatenated to form the new array without the unwanted element.

You can see the output in the screenshot below:

Remove Array Element by Index in PowerShell

Method 2: Using ArrayList

Since a standard PowerShell array is immutable in size, if you want to remove an item by index directly, you can use a PowerShell ArrayList. An ArrayList is a dynamic array, which means it can grow and shrink on demand. Here’s how to use it:

# Create an ArrayList
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange(@(1, 2, 3, 4, 5))

# Index to remove
$indexToRemove = 2

# Remove the element at the index
$arrayList.RemoveAt($indexToRemove)

# Display the ArrayList
$arrayList

The RemoveAt method is used here to remove the item at the specified index directly from the ArrayList.

Method 3: Using Where-Object Cmdlet

Another method to remove an element by index in PowerShell involves using the Where-Object cmdlet. This cmdlet allows you to filter out the element at the desired index:

# Original array
$array = 1, 2, 3, 4, 5

# Index to remove
$indexToRemove = 2

# Remove the element at the index using Where-Object
$newArray = $array | Where-Object { $_ -ne $array[$indexToRemove] }

# Display the new array
$newArray

This method compares each item ($_) in the array with the item at the index to remove, effectively filtering it out.

After I executed the above code using VS code you can see the output in the screenshot below:

powershell remove array element by index

Method 4: Using ForEach-Object with a Conditional

Another way of removing an element from an array by index in PowerShell is by using ForEach-Object with a conditional statement. Here is an example:

# Original array
$array = 1, 2, 3, 4, 5

# Index to remove
$indexToRemove = 2

# Initialize an empty array for the result
$newArray = @()

# Loop through the original array and exclude the index to remove
$array | ForEach-Object {
    if ($array.IndexOf($_) -ne $indexToRemove) {
        $newArray += $_
    }
}

# Display the new array
$newArray

This method iterates over each element in the original array and adds it to a new array unless it’s at the index to remove.

Check out the screenshot below for reference:

How to remove array element by index in PowerShell

Note: When removing elements by index, remember that if you remove an element from the middle of an array, the indexes of subsequent elements will change. This can be problematic if you’re removing elements in a loop. Always plan accordingly to avoid unexpected results.

Conclusion

Removing an element from an array by index in PowerShell can be achieved through various methods such as using an ArrayList, filter with Where-Object, or iterate with ForEach-Object, etc.

In this PowerShell tutorial, I have explained how to remove array elements by index in PowerShell using various methods.

You may also like: