QuickBlog: How to remove Array element (or range) with PowerShell…

As I look for a way that I could eliminate a single or a range of elements, I found many C# .NET code on how do it. But only one of the answer in many caught my eye. This answer was “System.Collections.ObjectModel.Collection“. This help me search deeper and of course found that there’s a couple of methods I could use if I create my Array as an ArrayList type.

This way I got available two methods I can use: .RemoveAt(  element# ) and .RemoveRange( AtElement# , HowMany# ) .

So, in scenario where you want to remove some elements of the array in PowerShell try using the following typed accelerator [System.Collections.ArrayList] .

Here’s a basic sample code:

[sourcecode language=”powershell”]
## – creating your Arraylist .NET object and creates 20 elements:
[System.Collections.ArrayList] $b = 1..20;

## – Here’s how you verify the type of object you created:
$b.GetType()

## – This example will remove Array element #4 and display the results:
$b.RemoveAt(4);
$b

## – This example will remove a range of 3 elements starting at element #7:
$b.removerange(7,3)
$b

[/sourcecode]

It’s all part of the beauty of .NET with PowerShell!