Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerTip: Use PowerShell to Remove an Item from an Array

$
0
0

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


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>