Skip to main content

How to Create an Array in PowerShell

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

An array keeps several values in one variable, in order. In PowerShell you make one by separating the values with commas, as in $colors = 'red', 'green', 'blue'. This guide creates an array, reads its items by position, adds an item and makes arrays of one item or none, with real output from PowerShell 7.6.10.

The about_Arrays page: "An array is a data structure that's designed to store a collection of items. The items can be the same type or different types."

Syntax​

Syntax
$<variable> = <value1>, <value2>, <value3>
$<variable>[<index>]
$<variable> += <value>
PartWhat it means
$<variable> =Saves the array in a variable, so you can use it later
<value1>, <value2>, <value3>The items, separated by commas. Text goes in quotes
$<variable>[<index>]Reads one item by its position, in square brackets: 0 is the first item, and -1 the last
$<variable> += <value>Adds an item at the end

The about_Arrays page: "To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator (=)."

The Example​

The example keeps three colors in an array named $colors. The commands run in the PowerShell 7.6.10 console.

Prerequisites​

  • PowerShell 7. This guide uses PowerShell 7.6.10: $PSVersionTable.PSVersion.ToString() printed 7.6.10.

Step 1: Create an Array​

PowerShell
$colors = 'red', 'green', 'blue'
$colors
$colors.Count

macOS-style code window titled PowerShell that saves the three colors 'red', 'green' and 'blue', separated by commas, in $colors, then prints $colors and $colors.Count.

Output
red
green
blue
3

The commas made one array of three items, and = saved it in $colors. Typing the array's name on its own printed every item on its own line. The about_Arrays page: "To display all the elements in the array, invoke the array name." Count returned the number of items, 3.

Step 2: Read an Item by Its Position​

PowerShell
$colors[0]
$colors[1]
$colors[-1]
Output
red
green
blue

macOS-style terminal window titled PowerShell where $colors = 'red', 'green', 'blue' is followed by $colors printing red, green and blue on three lines, $colors.Count printing 3, $colors[0] printing red, $colors[1] printing green and $colors[-1] printing blue.

Positions start at 0, so $colors[0] is the first color and $colors[1] the second. The about_Arrays page: "You can refer to the elements in an array using an index. Enclose the index number in brackets. Index values start at 0." A negative index counts from the end, which is why $colors[-1] returned blue: "Negative numbers count from the end of the array. For example, -1 refers to the last element of the array."

Step 3: Add an Item​

PowerShell
$colors = 'red', 'green', 'blue'
$colors += 'yellow'
$colors.Count
$colors[-1]
$colors.GetType().FullName
Output
4
yellow
System.Object[]

+= added yellow at the end: Count went up to 4, and the last item is now yellow. The about_Arrays page: "You can use the += operator to add an element to an array." It also warns: "When you use the += operator, PowerShell actually creates a new array with the values of the original array and the added value. This might cause performance issues if the operation is repeated several times or the size of the array is too big."

GetType().FullName printed the array's type, System.Object[]. The page: "When no data type is specified, PowerShell creates each array as an object array (System.Object[])."

Step 4: Make an Array of One Item or None​

PowerShell
$one = ,'red'
$one.Count
$one[0]
$also = @('red')
$also.Count
$none = @()
$none.Count
Output
1
red
1
0

A single value needs help to become an array. A comma in front of it is one way. The about_Arrays page: "The comma can also be used to initialize a single item array by placing the comma before the single item." The other way is @( ), the array subexpression operator, which made an array of one item from 'red' and an empty array, with a count of 0, from nothing at all. The page: "The array sub-expression operator creates an array from the statements inside it. Whatever the statement inside the operator produces, the operator places it in an array. Even if there is zero or one object."

Common Mistakes​

MistakeWhat happensDo this instead
Spaces instead of commas: $colors = 'red' 'green' 'blue'ParserError: Unexpected token ''green'' in expression or statement.Put a comma between the items
Counting from 1: $colors[3] for the third colorNothing: index 3 is past the end of a three-item array, and $null -eq $colors[3] returned TrueThe third item is $colors[2], and the last is $colors[-1]
+ without =: $colors + 'yellow'Four colors printed, but $colors.Count still returned 3: the new array wasn't savedUse +=
One value and no comma: $colors = 'red'$colors[0] returned r: $colors is a string, System.String, and the index picked its first letterWrite ,'red' or @('red')

Troubleshooting​

SymptomLikely causeFix
OperationStopped: Index was outside the bounds of the array.A value was assigned to a position past the end, as in $colors[3] = 'yellow'Add the item with +=
An index returns nothingThe position is past the end of the arrayCount from 0, or use -1 for the last item

FAQ​

How do I make an array of numbers?​

Use the range operator, two dots between the first and last number: $numbers = 1..5 made an array of 1, 2, 3, 4 and 5, and $numbers.Count returned 5. The about_Arrays page: "You can also create and initialize an array using the range operator (..)."

Why does reading past the end give no error?​

That's how PowerShell works. The Everything you wanted to know about arrays article: "In most languages, if you try to access an index of an item that is past the end of the array, you would get some type of error or an exception. PowerShell silently returns nothing." Writing past the end does fail: "If we try to update an item that is past the last element, then we get an Index was outside the bounds of the array. error."

Conclusion​

$colors = 'red', 'green', 'blue' made an array of three items: $colors[0] returned red and $colors[-1] returned blue. Separate the items with commas, count positions from 0, and add items with +=.

Additional Resources​