≡ Menu

Removing items from arrays in PowerShell

Arrays are powerful in PowerShell and makes many system administration tasks easy. The built-in array controls works great for most of the scenarios, but it lags in capability of easy removal of items from it. Well, don’t worry. Still you can do that easily in powershell by leveraging dotnet component system.collections.ArrayList.

To make array operations more flexible, you need to define your array with this particular dotnet component.

To initialize array..

$myarray = New-Object System.Collections.ArrayList

To add a item to it..

$myarray.add(“item1”)

To add another item..

$myarray.add(“item2”)

and so on…Now to remove a item from it…

$myarray.remove(“item1”)

If you don’t know the value and has just index number, still you can remove..

$myarray.removerange(1,1)

Here, 1 is the index number, that means, you are removing item from $myarray[1]

Reference :

  1. http://technet.microsoft.com/en-us/library/ee692802.aspx (using dotnet module)
  2. http://technet.microsoft.com/en-us/library/ee692798.aspx (using built-in powershell arrays)

Do you know any way to remove items from array without using a second one? 🙂