PowerTip: Use PowerShell to Remove an Item from an Array

Doctor Scripto

Summary: Use Windows PowerShell remove an item from an array.

Hey, Scripting Guy! Question How can I remove an item from an array?

Hey, Scripting Guy! AnswerUse the ArrayList class, which has the Remove() method (instead of using System.Array,
           which does not have this method):

PS > $ArrayList = New-Object System.Collections.ArrayList

PS > [void]$ArrayList.AddRange((1..10))

PS > $ArrayList

1

2

3

4

5

6

7

8

9

10

PS > 1..10 | % {If ($_%2){[void]$ArrayList.Remove($_)}}

PS > $ArrayList

2

4

6

8

10

0 comments

Discussion is closed.

Feedback usabilla icon