PowerShell | Arrays | Codecademy (2024)

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.

IndexValue
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 > $colors

red

brown

black

blue

Indexing

PowerShell offers flexibility when indexing items, such as:

  • Multiple indexes: Separate indexes with commas to print multiple items.

PS > $colors[0,2]

red

black

  • 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]

brown

black

blue

  • 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 and 1, are included.

PS > $colors[2..1]

black

brown

  • 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 works

3

5

5

4

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, 1

PS > $fibonacci_2 = 2, 3, 5

PS > $fibonacci_1 + $fibonacci_2

1

1

2

3

5

Multiplication Operator

The multiplication operator * copies the array a specified number of times.

PS > $fibonacci_2 * 2

2

3

5

2

3

5

Containment Operators

Containment operators check whether or not an item is in an array and returns a boolean.

OperatorSyntaxExample
-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, 5

PS > $fibonacci = $fibonacci -join "->"

PS > $fibonacci

0->1->1->2->3->5

PS > $fibonacci.GetType().Name

String

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 $_.

PowerShell | Arrays | Codecademy (2024)
Top Articles
Sermon: Lydia: A Model of Service and Hospitality - Acts 16 | Lifeway
Standard 5: Best interests and appropriateness - Assured Support
Where To Go After Howling Pit Code Vein
Riverrun Rv Park Middletown Photos
Lakers Game Summary
Jennifer Hart Facebook
Craigslist Vans
La connexion à Mon Compte
Ou Class Nav
Paketshops | PAKET.net
Zachary Zulock Linkedin
shopping.drugsourceinc.com/imperial | Imperial Health TX AZ
Craigslist Estate Sales Tucson
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
Phillies Espn Schedule
Oscar Nominated Brings Winning Profile to the Kentucky Turf Cup
Samantha Lyne Wikipedia
Dallas Cowboys On Sirius Xm Radio
Palm Coast Permits Online
Walmart stores in 6 states no longer provide single-use bags at checkout: Which states are next?
Carson Municipal Code
Odfl4Us Driver Login
Craigslist Pinellas County Rentals
Geometry Review Quiz 5 Answer Key
Finalize Teams Yahoo Fantasy Football
Culver's Flavor Of The Day Taylor Dr
Reptile Expo Fayetteville Nc
Dragger Games For The Brain
8005607994
Naya Padkar Gujarati News Paper
Meta Carevr
Truvy Back Office Login
Danielle Ranslow Obituary
Experity Installer
Robot or human?
Tgh Imaging Powered By Tower Wesley Chapel Photos
Umiami Sorority Rankings
20+ Best Things To Do In Oceanside California
Spn-523318
Linda Sublette Actress
11301 Lakeline Blvd Parkline Plaza Ctr Ste 150
A Comprehensive 360 Training Review (2021) — How Good Is It?
Nail Salon Open On Monday Near Me
Emily Browning Fansite
Satucket Lectionary
Powerboat P1 Unveils 2024 P1 Offshore And Class 1 Race Calendar
Strange World Showtimes Near Century Stadium 25 And Xd
Joblink Maine
Craigslist Anc Ak
Gear Bicycle Sales Butler Pa
How to Choose Where to Study Abroad
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6184

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.