How to Remove Items from an Array in PowerShell?

Do you need to remove items from an array in PowerShell? In this post, we’ll explore how to remove items from an array in PowerShell using various methods and examples.

To remove items from an array in PowerShell, create a new array without the item you want to remove or by using an ArrayList to which you can directly apply the Remove method. For example, to remove an item at index 0 from an array, you can use $array = $array[1..$array.length] to skip the first item. Alternatively, you can cast the array to an ArrayList and then use the RemoveAt method, such as [System.Collections.ArrayList]$arrayList = $array; $arrayList.RemoveAt(0).

Remove Items from an Array in PowerShell

Now, let us see how to remove items from an array in PowerShell using various methods.

Here is an example of a PowerShell array which is having a few items like below:

$myArray = @(1, 2, 3, 4, 5)

This creates an array with five integers in PowerShell.

1. Using The $Null Assignment Method

One of the simplest ways to remove an item from an array in PowerShell is to assign $Null to the index you want to remove:

$myArray = @(1, 2, 3, 4, 5)
$myArray[2] = $Null  # This will remove the item at index 2

You can see the output in the screenshot below.

Remove Items from an Array in PowerShell

However, this doesn’t completely remove the item; it replaces the item with a $Null value. To completely remove the item, you would need to filter it out:

$myArray = @(1, 2, 3, 4, 5)
$myArray = $myArray | Where-Object { $_ -ne $Null }

2. By Creating a New Array

If you’re dealing with a fixed-size array, you can’t directly remove an item from the PowerShell array. Instead, you create a new array that excludes the item you want to remove:

$myArray = @(1, 2, 3, 4, 5)
$myArray = $myArray[0..1] + $myArray[3..4]  # This will exclude the item at index 2

This method involves concatenating slices of the original array, omitting the element you want to remove.

I have executed the script using VS code, and you can see the output in the screenshot below:

PowerShell Remove Items from an Array

3. Using ArrayLists

Another way to remove items from an array is by using a PowerShell ArrayList, a dynamic array that provides methods for adding and removing items:

$myArray = @(1, 2, 3, 4, 5)
$arrayList = New-Object System.Collections.ArrayList
$arrayList.AddRange($myArray)  # Add the items of $myArray to $arrayList
$arrayList.Remove(3)  # This will remove the first occurrence of the value 3

ArrayLists are not arrays per se, but they can be very useful when you need to manipulate collections of items dynamically.

4. Using the RemoveAt Method

If you know the index of the item you want to remove, you can use the RemoveAt method of an ArrayList:

$arrayList.RemoveAt(2)  # This will remove the item at index 2

This method is specific to ArrayLists and cannot be used on regular PowerShell arrays.

Let’s look at some examples of removing items from arrays in PowerShell.

Remove items from PowerShell Array by Value

There might be a situation where you might need to remove an item from a PowerShell array by value. Here is a complete PowerShell script that will remove all occurrences of the value 3.

$myArray = @(1, 2, 3, 4, 5)
$myArray = $myArray | Where-Object { $_ -ne 3 }  # Removes all occurrences of the value 3

Remove Items from an Array in PowerShell by Index

Here is another example where I need to remove items from an array in PowerShell by its index.

$myArray = @(1, 2, 3, 4, 5)
$indexToRemove = 2
$myArray = $myArray[0..($indexToRemove-1)] + $myArray[($indexToRemove+1)..($myArray.Length-1)]

Conclusion

Removing items from an array in PowerShell can be done in several ways, depending on whether you’re working with a fixed-size array or need more dynamic capabilities.

In this PowerShell tutorial, I have explained how to remove an item from an array in PowerShell by using the below methods:

  1. Using The $Null Assignment Method
  2. By Creating a New Array
  3. Using ArrayLists
  4. Using the RemoveAt Method

I have also explained the below examples:

  • Remove items from PowerShell Array by Value
  • Remove Items from an Array in PowerShell by Index

You may also like: