Declare and initialize an array

Arrays contains a multple values of same or different types

Empty arrays declared using below syntax

$words = @()

Variable is declared with name and prefix with $. Assigned with @(), that tells array length is empty.

You can also initialzie an array using @() with list of elments comma separated

$words = @("one","two","three");

$words variable stores an array of three strings. same syntax can be written using without parenthesis

$words = "one","two","three"

Elements need not be of same type, You can have multiple types of elements.

$words = "one",2,3.0

Above array holds string, integer and floating numbers

Another, typed array can be declare using [type]

$words = @("one","two","three");

# Positive Index
Write-Host $words[0] # one
Write-Host $words[1] # two
Write-Host $words[2] # three
Write-Host $words[3] #  empty, Not Found
# Negative Index
Write-Host $words[-1] # three
Write-Host $words[-2] # two
Write-Host $words[-3]# one
Write-Host $words[-4]#  empty, Not Found

# Prints an array
Write-Host $words # one two three
  • Access array elements.
  • Array elements stored sequential elements.

Accessed from start or end.

elements in array are accessed using index or ranges

  • Use Index
$array[index]

if you want to access beginning, use positive index starts from zero.

if you want to access from end, use negative index starts from -1.

For example, $words=“one”,“two”,“three” declared,

$words[0] returns first element i.e one $words[1] returns second element i.e two $words[-1] returns last element i.e third $words[-21] returns second element i.e two

Elements accessed using [index],where index starts from zero.

$words[0] returns first element i.e one

  • Using Ranges

Another way to access range of elements in an array

$array[start..end]

range specificed using start..end, which means returns an element from start to end with inclusive of end element.

$words = @("one","two","three");

Write-Host $words[0..1] # one two
Write-Host $words[1..2] # two three
Write-Host $words[-1..2] # three one two three
Write-Host $words[-1..1] # three one two
Write-Host $words[-1..-2] # three two
  • Slicing syntax

Single element can be accessed using $array[index]. Slicing approach allows you to retrive multiple elements using index in a single call.

$array[index1, index2, so on ]

index can be positive or negative, comma separated. you can also use + to get the mentioned index elements For example

$words = @("one","two","three");

Write-Host $words[0,1,2] # one two three
Write-Host $words[1,2] # one two
Write-Host $words[2,1,0] # three two one
Write-Host $words[0,1+1,2] # one two two three
Write-Host $words[0,0+2,2] # one one three threeyes

$words[2,1,0] returns an element at 2, 1, 0 index positions i.e three two one Write-Host $words[0,0+2,2] returns the one one three threeyes, 0+2 returns the elements at one and third position

Update or add elements in an array

You can update the elements in an array using below syntax

$array[index] = value

Here is an example

#Create an array
$numbers = @(1,2,3);
$numbers[0]=11
$numbers[1]=12
$numbers[2]=13


Write-Host $numbers # 11 12 13
$numbers[2]=14
Write-Host $numbers # 11 12 14

Since array is fixed in nature, if you try to add new elements, it will throw an IndexOutOfRangeException error.

#Create an empty array
$numbers = @();
$numbers[0]=11
  • $numbers[0]=11
  •   + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
      + FullyQualifiedErrorId : System.IndexOutOfRangeException
    

Multi dimensional Arrays

Powershell Supports multi dimainsional arrays, Examples are matrix. It contains each element as an array or nested array, also called array of arrays.

For example, Matrix is an 2 dimensional array, that contains rows and columns

# Declare and initialize two dimensional array
$matrix = @(
  @("one","two","three"),
  @("four","five","six")
)

# Prints entire matrix
write-host $matrix # one two three four five six

# Prints first row
write-host $matrix[0] # one two three

# prints second column second elment
write-host $matrix[1][1] # five

#Update an element
$matrix[1][1]=6
# Prints entire matrix after modification
write-host $matrix #one two three four 6 six

Access the elemens with matrix

printing $matrix displays one two three four five six printing $matrix[0] displays one two three printing $matrix[1][1] displays five