Powershell

Table of Contents

Arrays

Arrays are a data structure that consists of a collection of values either as variables or objects that have an unique numbered identifying index or key. An array is stored using a computed mathematical formulae a tuple.

Within programming you will often hear the word Matrix. A matrix (plural: matrices) is a rectangular array that can consist of numbers, symbols or expressions. In Computing a matrix is known as a 2 dimensional array. Within Powershell an Array is an matrix. Which can also be called a Vector or Tuple.

Arrays are fundamentally a kind of data type provided by most high-level programming languages that consists of a collection of values or variables that can be selected by one or more indices computed at run-time.

An Example Array in Powershell would be
$array = "1","Hello","-1"

In the above example an empty object named array has been defined with having three (3) records; "1","Hello","-1" that are separated by an comma value and enclosed in double quotation marks.

Any object or value can be included in an array provided it is encapsulated in quotation marks and separated by a comma for each record.
To output the contents of the array you may type the object name. $array. outputting all records

1
Hello
-1


To see the array type you may use the extended data method .gettype()
$array.GetType()




To select an individual record you can call the index value. To understand index sequence look at the image below.



The first Index value is zero (0), which is represented by the record one (1) in array $array. The second record which is a string "Hello" is stored in Index record one (1). The last record minus one (-1) is stored in index record two (2).

Knowing this we can output the value of a particular array record with the index value.

$array[0]
1
$array[1]
Hello
$array[2]
-1


You can also reference multiple arrays by manipulating the contents of the square brackets. You may use double period characters (..) to separate index values. Or specify an objects extended data type count method (.count) which exists for most objects.

$array[1..2]
Hello
-1
$array[1..$array.Count]
Hello
-1


To add objects/values to an array you may use the += operator of an object

$array+="Test"


To count the new values of an array you may use the .count method

$array.Count
4


Conversely you are not able to subtract using -= as an generic array does not have a subtract method. To remove objects/values from an array you will have to create a new array and use the not equal to operator (-ne).

$array
1
Hello
-1
Test
$array = $array –ne "Test"
$array
1
Hello
-1


The problem with the above example is that you can accidently remove all values of the –ne matching value "Test" which creates a limitation of the generic array data type.

Arrays are fixed index length objects. To find that an array is of a fixed length you may use call the.IsFixedSize property

$array.IsFixedSize
True


To create a dynamically sized array to add, subtract and modify, then you may instead chose to use a list data type which will allow you to use the get .subtract() method. This is a more memory efficient method of constructing an array.

First instantiate the object using the new-object command then call the .NET class System.Collections.Generic.List[System.Object] and start using the .add and .subtract() methods.

$array = New-Object 'System.Collections.Generic.List[System.Object]'
$array.Add("Test")
$array.Add("Hello")
$array.Subtract("Test")


There is also an method of System.Collections.Generic.List class method .ToArray() which allows you to cast the list data type object into an array data type.

$array.ToArray()


Visit next page to learn about - .COM