Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

7.1 Create an Array or List of Items

Problem

You want to create an array or list of items.

Solution

To create an array that holds a given set of items, separate those items with commas:

PS > $myArray = 1,2,"Hello World"
PS > $myArray
1
2
Hello World

To create an array of a specific size, use the New-Object cmdlet:

PS > $myArray = New-Object string[] 10
PS > $myArray[5] = "Hello"
PS > $myArray[5]
Hello

To create an array of a specific type, use a strongly typed collection:

PS > $list = New-Object Collections.Generic.List[Int]
PS > $list.Add(10)
PS > $list.Add("Hello")
Cannot convert argument "0", with value: "Hello", for "Add" to type "System
.Int32": "Cannot convert value "Hello" to type "System.Int32". Error:
"Input string was not in a correct format.""

To store the output of a command that generates a list, use variable assignment:

PS > $myArray = Get-Process
PS > $myArray

Handles  NPM(K)    PM(K)      WS(K) VM(M)  CPU(s)     Id ProcessName
-------  ------    -----      ----- -----  ------     -- -----------
    274       6     1316       3908    33           3164 alg
    983       7     3636       7472    30            688 csrss
     69       4      924       3332    30    0.69   2232 ctfmon
    180       5     2220       6116    37           2816 dllhost
(...)

To create an array that you plan to modify frequently, use an ArrayList, as shown by Example 7-1.

Example 7-1. Using an ArrayList to manage a dynamic collection of items
PS > $myArray = New-Object System.Collections.ArrayList
PS > [void] $myArray.Add("Hello")
PS > [void] $myArray.AddRange( ("World","How","Are","You") )
PS > $myArray
Hello
World
How
Are
You
PS > $myArray.RemoveAt(1)
PS > $myArray
Hello
How
Are
You

Discussion

Aside from the primitive data types (such as strings, integers, and decimals), lists of items are a common concept in the scripts and commands that you write. Most commands generate lists of data: the Get-Content cmdlet generates a list of strings in a file, the Get-Process cmdlet generates a list of processes running on the system, and the Get-Command cmdlet generates a list of commands, just to name a few.

Note

The Solution shows how to store the output of a command that generates a list. If a command outputs only one item (such as a single line from a file, a single process, or a single command), then that output is no longer a list. If you want to treat that output as a list even when it’s not, use the list evaluation syntax, @(), to force PowerShell to interpret it as an array:

$myArray = @(Get-Process Explorer)

When you want to create a list of a specific type, the Solution demonstrates how to use the System.Collections.Generic.List collection to do that. After the type name, you define the type of the list in square brackets, such as [Int], [String], or whichever type you want to restrict your collection to. These types of specialized objects are called generic objects. For more information about creating generic objects, see “Creating Instances of Types”.

For more information on lists and arrays in PowerShell, see “Arrays and Lists”.

See Also

“Arrays and Lists”

“Creating Instances of Types”