Adding elements to a list(pshell)

Adding elements to an array using += actually creates a new array with the elements from the original array and adds the new element to it.

A better practice is to create a list instead:

$outItems = New-Object System.Collections.Generic.List[System.Object]

Adding items to the list is done like this:

$outItems.Add(1)
$outItems.Add("hi")

And if you want to convert the list to an array it’s done like this:

$outItems.ToArray()

 

Advertisements
Posted in array, howto, powershell

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: