A powershell array is a data structure that can store a collection of items. Unlike powershell variables, these can store multiple single or multiple items. The items can be the same type or different types. The example below illustrates the preceding statement.
PS C:\> $array = 2,"String", $true
PS C:\> $array[0].GetType().Name
Int32
PS C:\> $array[1].GetType().Name
String
PS C:\> $array[2].GetType().Name
Boolean
If you didn't understood above commands, don't worry this post will explain what's in that square bracket and what does that GetType() mean, just read on. Here, the variable $array store multiple items. When we access each item in the array and get the type of each element, we can see different datatypes. First element is integer, second is a string and third is a boolean datatype. We will further see in detail how we can access the elements of an array. As of now, just remember that arrays can store multiple elements of same or different datatypes.
Defining and initializing array:
One of the ways in which we can define array in powershell is by directly assigning the values to a variable just like we saw in the previous example. Another way to define an array is be using the @(..) syntax. Using this syntax we can initialize and empty array and later we can add elements in the array.
$InitializedArray = @()
foreach ($alphabet in ("a","b","c")) {$InitializedArray += $alphabet}
Here, in the first command, we initialized an empty array. In the second command we added 3 elements "a", "b" and "c" in the initialized array.
The third way is to use the add method of the array object. This is the most efficient way of working with arrays. We have a detailed explanation of this method in this blog post Add and remove elements from Powershell Array
Accessing elements of an array:
Once you have your array declared you may need to access the values stored inside the array. These values which are stored in array are called elements of an array and the place in which they are stored is called the index. Indexing starts from 0. It means if you wish to access the first element of an array you will need to mention number 0. Elements of an array can be accessed using square bracket notation. We will see this in the example below.
We will first initialize an array Fruits and display the values stored in the array
PS C:\> [System.Collections.ArrayList]$Fruits = @("Apple","Mango","Grapes")
PS C:\> $Fruits
Apple
Mango
Grapes
Here the first element is "Apple" but since the indexing starts from 0, we will need to supply index 0 in square brackets if we need to access the first element.
PS C:\> $Fruits[0]
Apple
Similarly, for accessing third element we need to supply index 2
PS C:\> $Fruits[2]
Grapes
Arrays can be accessed from backwards too. If we need to access last element of an array we need to supply a negative number starting from –1 and so on.
PS C:\> $Fruits[-1]
Grapes
Accessing properties and methods of an array:
An array has some properties and methods. These properties and methods can be used to perform various operations. Properties and methods of an array or variable can be access or invoked using the dot notation. Syntax would be $Variable.PropertyName or $Variable.MethodName.
Here are some of the properties of an array:
We can check the count of an array using the count property:
PS C:\> $Fruits.Count
3
Then we have the length property which will fetch the length of each element in an array:
PS C:\> $Fruits.Length
5
5
6
Here are some of the methods of an array:
The Remove method will remove the element passed on as a parameter.
PS C:\> $Fruits.Remove("Grapes")
PS C:\> $Fruits
Apple
Mango
Add method will add an element at the last.
PS C:\> $Fruits.Add("Orange")
2
PS C:\> $Fruits
Apple
Mango
Orange
RemoveAt method will remove the element from the place where index number was passed on as parameter. Here the element at index 1 which is "Mango" will be removed
PS C:\> $Fruits.RemoveAt(1)
PS C:\> $Fruits
Apple
Orange
Conversely Insert method will add an element at the specified index.
PS C:\> $Fruits.Insert(1,"Mango")
PS C:\> $Fruits
Apple
Mango
Orange
The IndexOf method will get the place of the specified element.
PS C:\> $Fruits.IndexOf("Apple")
0
GetType is another method that can be used to see the type of data store in a variable.
PS C:\> $Fruits.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ArrayList System.Object
There are many other properties and methods of a variable / array. You can check them by piping the variable to the get-member command as given below.
PS C:\> $Fruits | Get-Member
Comments