HOWTO · PowerShell
How to Remove Item From an Array in PowerShell
Learn when to use an ArrayList or filter an array to remove items by value, index, range, or condition in PowerShell.
On this page
To remove an item from a PowerShell collection, first identify whether you have a fixed-size array or an ArrayList. An array cannot shrink in place. An ArrayList can be changed with Remove(), RemoveAt(), or RemoveRange(). To keep selected items from an array, use the ForEach-Object cmdlet and assign its output back to a variable.
The examples below show removal by value, zero-based index, contiguous range, and condition. The ArrayList methods change the existing collection; filtering produces a new result.
Choose between a PowerShell array and an ArrayList
PowerShell array literals such as $items = @('apple', 'banana', 'cherry') create fixed-size arrays. You can replace an element, but you cannot remove one in place and reduce the array’s length. For a collection that needs repeated additions and removals, use System.Collections.ArrayList. Its Remove(), RemoveAt(), and RemoveRange() methods mutate the same collection.
Use an array when you mainly need to read or filter values, or when the collection’s size is stable. Use an ArrayList when you need to change membership directly. If you already have an array, filtering and assigning the result is usually the clearest way to get a smaller array.
Remove an item by value with Remove()
Call Remove() with the value to delete. It removes the first matching item from an ArrayList; it does not remove every duplicate. The following example starts with a list of fruit and removes banana.
Run this example:
$items = [System.Collections.ArrayList]::new()
[void]$items.Add('apple')
[void]$items.Add('banana')
[void]$items.Add('cherry')
$items.Remove('banana')
$items
apple
cherry
The call changes $items itself. If the value might occur more than once and all matching entries should go, filter the collection by a condition instead.
Remove an item by index with RemoveAt()
Use RemoveAt() when the position is known. ArrayList indexes start at zero, so index 1 refers to the second item. Removing an item shifts later items left and changes their indexes; determine the target index from the current collection immediately before removal.
$items = [System.Collections.ArrayList]::new()
[void]$items.Add('apple')
[void]$items.Add('banana')
[void]$items.Add('cherry')
$items.RemoveAt(1)
$items
apple
cherry
The example removes the item at index 1 from this three-item list. Check that an index is between 0 and $items.Count - 1 before calling the method. An index outside that range causes a runtime error, and removing an earlier item changes the indexes of the items that follow it.
Remove a range with RemoveRange()
RemoveRange(startIndex, count) removes a contiguous group from an ArrayList. The first argument is the zero-based index where removal begins; the second is the number of items to remove, not an ending index. For example, start index 1 and count 2 remove the second and third items.
$items = [System.Collections.ArrayList]::new()
[void]$items.Add('apple')
[void]$items.Add('banana')
[void]$items.Add('cherry')
[void]$items.Add('date')
[void]$items.Add('elderberry')
$items.RemoveRange(1, 2)
$items
apple
date
elderberry
The range must fit in the current list. For a positive count, the start index must identify an existing item and the count cannot extend past the end. A count of zero removes nothing and may use a start index from 0 through the current Count, including Count itself. Check both arguments against the current Count before calling RemoveRange() when their values come from user input or calculations.
Filter an array with ForEach-Object
ForEach-Object is a cmdlet, not a method. In a pipeline, it processes each input item and emits the items that pass the condition. Assign that output to a variable to create the filtered array; the source array remains unchanged unless you reassign the result to the same variable.
$items = @('apple', 'banana', 'cherry', 'date', 'elderberry')
$items = $items | ForEach-Object {
if ($_ -notlike '*a*') {
$_
}
}
$items
cherry
elderberry
Here the condition keeps strings that do not contain a, so the retained values are cherry and elderberry. Change the condition to match the values you want to keep. This approach is useful when removal depends on a property or rule rather than one known value or position.
Check the result and avoid common mistakes
Choose the operation that matches what you know: use Remove() for the first matching value in an ArrayList, RemoveAt() for one known zero-based position, and RemoveRange() for consecutive positions. Use ForEach-Object when a condition determines which items remain, then assign its output if you want to replace the original array variable.
Remember that the three ArrayList methods mutate the list, while filtering returns retained items for assignment. If you need to remove every occurrence of a value, a condition-based filter is more appropriate than a single Remove() call. After index-based changes, recalculate positions from the updated collection, and validate index and range arguments against the current count.