December 2nd, 2014

PowerTip: Use PowerShell to Remove an Item from an Array

Doctor Scripto
Scripter

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.