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
Small Business Insurance Ontario - APOLLO Insurance
Small Business Insurance in Ontario | Online Quotes | Bullfrog
Sprinter Tyrone's Unblocked Games
Cottonwood Vet Ottawa Ks
Angela Babicz Leak
Wellcare Dual Align 129 (HMO D-SNP) - Hearing Aid Benefits | FreeHearingTest.org
Undergraduate Programs | Webster Vienna
Localfedex.com
His Lost Lycan Luna Chapter 5
Oriellys St James Mn
Wordle auf Deutsch - Wordle mit Deutschen Wörtern Spielen
Playgirl Magazine Cover Template Free
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Osborn-Checkliste: Ideen finden mit System
Daylight Matt And Kim Lyrics
Earl David Worden Military Service
Gran Turismo Showtimes Near Marcus Renaissance Cinema
Hampton University Ministers Conference Registration
8000 Cranberry Springs Drive Suite 2M600
Weldmotor Vehicle.com
Hctc Speed Test
Malluvilla In Malayalam Movies Download
Yale College Confidential 2027
manhattan cars & trucks - by owner - craigslist
Gopher Hockey Forum
Uncovering the Enigmatic Trish Stratus: From Net Worth to Personal Life
91 Octane Gas Prices Near Me
Motor Mounts
APUSH Unit 6 Practice DBQ Prompt Answers & Feedback | AP US History Class Notes | Fiveable
35 Boba Tea & Rolled Ice Cream Of Wesley Chapel
Selfservice Bright Lending
Frostbite Blaster
PA lawmakers push to restore Medicaid dental benefits for adults
Foolproof Module 6 Test Answers
Heavenly Delusion Gif
D-Day: Learn about the D-Day Invasion
Telugu Moviez Wap Org
968 woorden beginnen met kruis
Newsweek Wordle
UT Announces Physician Assistant Medicine Program
Cch Staffnet
Wolf Of Wallstreet 123 Movies
Dancing Bear - House Party! ID ? Brunette in hardcore action
Headlining Hip Hopper Crossword Clue
Minecraft: Piglin Trade List (What Can You Get & How)
Random Warzone 2 Loadout Generator
Is My Sister Toxic Quiz
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Image Mate Orange County
Samantha Lyne Wikipedia
Lake County Fl Trash Pickup Schedule
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.