Contents

PowerShell Array and ArrayList - storing multiple items as a one variable

Contents

Array is a basic data structure that allows to store multiple items in one variable. In this short video, I’ll show you how to create a basic array, populate it with items, how to add, access and modify items within array.

In addition, I’ll present to you how to use ArrayList - which is much more flexible array, especially for adding more items.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Link to the video: https://youtu.be/rvGd8kxXlVc

# What is an array and why do I need it?
<#
Array is data structure that allows to collect multiple items under one collection
so that these items can be accessed individually, iterated over or even changed.

In other words, arrays allow us to stash bunch of values under one variable,
and work with it.
#>

# How to create an empty array
$stuff = @()
$stuff
"I've got lot's of stuff: $($stuff.Count)"

# Create an array with some stuff
$stuff = @("Fork", "Knife", "Salt")
$stuff
"Now really, I've got lot's of stuff: $($stuff.Count)"

# Comma separating also works for creating an array
$stuff = "Table", "Cup", "Coffe", "Milk"
$stuff

###############################
# Accessing items in an array #
###############################

#Arrays are 0 based indexed
$stuff[0]
$stuff[2]
$stuff[-1]
$stuff[2,1,2,0]
$stuff[0..2]
$stuff[2..0]

# Accessing items outside of an array is silent
$stuff[20]
[bool]$stuff[20]

# Mixing different types in array
$mix = @("Hi", 74, (Get-Date), "Bye")
$mix

# More often, we can store projects
$people = @(
    [pscustomobject]@{Name = "John"; Email = "john@john.com"}
    [pscustomobject]@{Name = "Tony"; Email = "tony@tony.com"}
    [pscustomobject]@{Name = "Fiber"; Email = "fiber@optics.com"}
)
$people
$people[-1]
$people.Email
$people | Where-Object {$_.Name -eq "John"}
$people.Where({$_.Name -eq "John"}).Email

# Null will throw an error 
$IDidntCreateThis[3]

###########
# Looping #
###########

$domains = @('bbc.com', 'github.com', 'youtube.com', 'blah.blah')
$ProgressPreference = 'SilentlyContinue'
foreach ($domain in $domains) {
    $Result = Test-NetConnection -ComputerName $domain -InformationLevel Quiet
    "$domain is pinging: $Result"
}

$domains | ForEach-Object {
    $Result = Test-NetConnection -ComputerName $_ -InformationLevel Quiet
    "$_ is pinging: $Result"
}

$domains.ForEach{
    $Result = Test-NetConnection -ComputerName $_ -InformationLevel Quiet
    "$_ is pinging: $Result"}


for ($i = 0; $i -lt $domains.Count; $i++) {
    $Result = Test-NetConnection -ComputerName $domains[$i] -InformationLevel Quiet
    "$($domains[$i]) is pinging: $Result"
}

###################
# Updating values #
###################

$Week = @("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
$Week[2] #Now it is Wednesday
$Week[2] = "Boo"
$Week[2]
$Week

# Update out of index throws error
$Week[8] = "Hi"

#############
# Operators #
#############

# Join
$Week
$Week -join "|"
$Week -join $null

# In
"Tuesday" -in $Week

# Match
$Week -match "es"

###################
# Adding to array #
###################

# Arrays have by definition fixed size in memory, in other words 
# you shouldn't be able to add more items to it. 
# PowerShell however allows you to do that, hiding all complexity
# by creating a new array, copying all existing item, adding new one
# and finally removing old array. 
# If you plan to add items to your array, look at ArrayList and List below

# Adding elements to array
$Week
$Week.Add("January") #this throws an error
$Week += "January"
$Week

# Adding arrays together
$ArrayA = 1..10
$ArrayB = 11..20
$ArrayA + $ArrayB

#############
# ArrayList #
#############

#Adding items to ArrayList
$alist = [System.Collections.ArrayList]::new() #This is .Net framework
$alist.Add("January")
[void]$alist.Add("February") #Void stops prevents writing to console
$alist

# Removing from ArrayList
$alist.Remove($alist[1])
$alist