Arrays
Arrays are a data structure that holds a collection of items. Items can be of the same type or multiple types.
Creating an Array
Separating items by commas (,
) is the simplest way to create an array in PowerShell.
$my_arr = 25, "Codecademy", 1, $False
Alternatively, the array subexpression operator @( )
can be used. Anything placed within the parentheses is treated as an item of the array.
# 3 elements$arr_1 = @($True, 5, (Get-Date).DateTime)# Empty Array$arr_2 = @( )# Multi-line Array$arr_3 = @("Uno""Dos""Tres")
Accessing Array Items
The items in an array are accessed using their index, or their position in the array. Consider the array:
$colors = "red", "yellow", "black", "blue"
The indexes in PowerShell start at 0
.
Index | Value |
---|---|
0 |
"red" |
1 |
"yellow" |
2 |
"black" |
3 |
"blue" |
Brackets [ ]
are used to access an item in an array. To access "black"
in the $colors
array, for example:
PS > $colors[2]black
Updating Array Items
Items can be updated — or changed – by utilizing indexes. To change the color yellow
to brown
, for example:
PS > $colors[1] = "brown"PS > $colorsredbrownblackblue
Indexing
PowerShell offers flexibility when indexing items, such as:
- Multiple indexes: Separate indexes with commas to print multiple items.
PS > $colors[0,2]redblack
- Range operator
..
: Prints all items between two indexes (inclusive).
Note: This syntax is inclusive since the stop index
3
is included.
PS > $colors[1..3]brownblackblue
- Reverse range: Use range operator from a higher index to a lower index to print items in reverse order (inclusive).
Note: This syntax is inclusive since both the start and stop indexes,
2
and1
, are included.
PS > $colors[2..1]blackbrown
- Negative indexes: Items are referenced in reverse order where the last item has an index of
-1
.
PS > $colors[-1]blue
Iteration
Each array object has a method called ForEach
which can be utilized to perform the same action on each of its items. PowerShell defines the variable $PSItem
or just underscore _
to refer to each item in the array.
PS > $colors.ForEach({ $PSItem.Length }) # $_.Length also works3554
The example above is printing the length of each string of our colors
array. red
has a length of 3
, brown
is 5
, and so on.
Array Operators
Addition Operator
The addition operator +
concatenates – or combines – two arrays.
PS > $fibonacci_1 = 0, 1, 1PS > $fibonacci_2 = 2, 3, 5PS > $fibonacci_1 + $fibonacci_2011235
Multiplication Operator
The multiplication operator *
copies the array a specified number of times.
PS > $fibonacci_2 * 2235235
Containment Operators
Containment operators check whether or not an item is in an array and returns a boolean.
Operator | Syntax | Example |
---|---|---|
-contains |
<array> -contains <item> |
$fibonacci -contains 4 returns False . |
-notcontains |
<array> -notcontains <item> |
$fibonacci -notcontains 4 returns True . |
-in |
<item> -in <array> |
5 -in $fibonacci returns True . |
-notin |
<item> -notin <array> |
5 -notin $fibonacci returns False . |
-join
The -join
operator combines the items in an array into a string separated by a character or a string. Consider the following example:
PS > $fibonacci = 0, 1, 1, 2, 3, 5PS > $fibonacci = $fibonacci -join "->"PS > $fibonacci0->1->1->2->3->5PS > $fibonacci.GetType().NameString
Strongly Typed Arrays
Types can be casted onto arrays to force each item in the array to adhere to that type.
PS > [String[]]$fruits = "apple", "banana", "kiwi"
Objects Arrays
Arrays can hold objects.
$dogs_arr = @([PSCustomObject]@{Name = 'Rufus'; Age = 10}[PSCustomObject]@{Name = 'Miku'; Age = 2})
Each object in the array as well as its properties and methods can be accessed individually.
PS > $dogs_arr.ForEach({ $PSItem.Name + " is " + $PSItem.Age + " years old."})Rufus is 10 years old.Miku is 2 years old.
Note:
$PSItem
can be replaced with its shorthand alias$_
.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PowerShell on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn PowerShell
Jump into PowerShell through interactive lessons on variables, operators, control flow, objects, arrays, and functions.Beginner Friendly6 hours