Updated March 8, 2023
Introduction to PowerShell add to an array
PowerShell array is the collection of the items of the single or the different data types, and the method add to the array means adding the new items at the particular index of the array or the array list with the different methods like the plus equal operator (+=) or Add() method and also the multiple arrays with the single or the different data types can be merged into one to form a single array.
Syntax:
1. The syntax for the Plus Equal Operator (+=).
$array1 = @(value1, Value2, value3)
$array1 += "Value4"
2. With the Add() method of the array list.
$arr = [System.Collections.ArrayList]@('Value1', 'Value2', 'Value3')
$arr.Add('Value4')
How does PowerShell add to array Works?
Array not only in PowerShell but in all the programming languages has a similar structure, and it works on the indexing structure.
Below is the sample array declaration.
Code:
$arr = @()
Let’s check its type.
Code:
$arr.GetType()
Output:
It shows the BaseType System.Array and the Name as Object[] mean we can add multiple objects in the array.
Once we declare the array, if we need to add the values to the array, we can use the below two methods.
Code:
$arr += "Hello"
$arr += "PowerShell"
$arr
Output:
If we check here, they all are added at the specific index incremented by one.
Code:
$arr[0]
$arr[1]
Output:
There won’t be any value at index 2 because we haven’t added it yet.
Code:
$arr[2]
Output:
Another method we can use is the Add() method, but it doesn’t apply to the fixed-size array.
Code:
$arr.Add('Azure')
Output:
PowerShell arrays are of a fixed size, so when adding or removing the array to the list, it destroys the existing array and creates a new one, and array with the fixed size doesn’t allow the Add() or Remove() method; instead, we can use the += (Plus Equal) for add and -= (Minus Equal) operators for add and the remove operation.
To check if the array has a fixed size.
Code:
$arr.IsFixedSize
Output:
Similarly, if we take any cmdlet output and it will be stored as an array; and it will always be a fixed size.
Code:
$ser = Get-Service
$ser.GetType()
$ser.IsFixedSize
Output:
There is another method that we can add the values to the array is using ArrayList. ArrayList has a different declaration method, as shown below.
Code:
$arrlist = [System.Collections.ArrayList]@("Hello","PowerShell")
$arrlist
Output:
If we check the type of this array list, it is System.Object.
Code:
$arrlist.GetType()
Output:
ArrayList allows using Add() method to add the values to the array list.
Code:
$arrlist.Add("Azure")
We can add the output here because the ArrayList is not the fixed size.
Code:
$arrlist.IsFixedSize
Output:
Examples of PowerShell add to array
Given below are the examples of PowerShell add to array:
Example #1
Adding values to the array.
In this example, we are adding one more animal, “Cow”, to the Array $Animals.
Code:
$Animals = @()
$Animals = "Cat" , "Dog" , "Tiger"
$Animals += "Cow"
$Animals
C:\Temp\TestPS.ps1
Output:
Example #2
Using the addition operator to merging two arrays.
Here, we are going to merge the two arrays to the empty array using the ‘+’ (plus) operator.
Code:
$PetAnimals = @( "Cow" , "Dog" )
$WildAnimals = @( "Tiger" , "Lion" )
$Animals = @()
$Animals = $PetAnimals + $WildAnimals
$Animals
Output:
Example #3
Using Arraylist to add the value.
In the below example, we are creating an arraylist and adding the two values to the list.
Code:
$arrlist = New-Object -TypeName System.Collections.ArrayList
$arrlist.Add("One")
$arrlist.Add("Two")
$arrlist
Output:
To add more values, we can either use, Plus assignment ‘+=’ method or the Add() method as shown below.
Code:
$arrlist += "Three"
$arrlist += "Four"
$arrlist
Output:
Example #4
Using loop to add the array values.
In this example, we will have the list to enter the data into the array, and we will use a foreach loop to add the values to the array.
Code:
$list = "One", "Two", "Three"
$arr = @()
foreach( $item in $list ){
$arr += $item
}
$arr
Output:
To add the values to the ArrayList using the foreach loop.
Code:
$list = "One", "Two", "Three"
$arrlist = New-Object -TypeName System.Collections.ArrayList
foreach( $item in $list ){
$arrlist.Add($item)
}
$arrlist
Output:
Example #5
Using the Indexing method to insert the values to the array.
Suppose we have the array below.
Code:
$arr = @("One", "Two", "Three")
Below is the array index for this declared array.
Code:
Write-Output "0th Index Value: $($arr[0])"
Write-Output "1st Index Value: $($arr[1])"
Write-Output "2nd Index Value: $($arr[2])"
Write-Output "3rd Index Value: $($arr[3])"
Output:
The 3rd index is empty because we haven’t declared anything yet. So, when we try to add the value to it, we get the below error.
Code:
$arr[3] = "four"
Or
Code:
$arr[3] += "four"
Output:
Because that index is unknown to the array or the similar thing with ArrayList. The workaround for this method is to assign the null values to the index initially, but this makes the array with the fixed size count.
Code:
$count = 5
$arr = @()
for($i=0; $i -lt $count; $i++){
$arr += ""
}
We are declaring the 5 indexes for the array and assigning the blank values to it, so it makes the array size 5 without any values that is the disadvantage of it. So we can now assign values to it.
Code:
$arr[0] = "One"
$arr[1] = "Two"
$arr[3] = "Four"
$arr
Output:
Please note here, indexes 2 and 4 are empty. This method is not suitable.
Example #6
Adding host file entry using String array.
This example will add the entry to the host file after retrieving the output to the string array.
Code:
$hostfile = Get-Content C:\Windows\System32\drivers\etc\hosts
$hostfile += "8.8.8.8 Google.com"
$hostfile | Set-Content C:\Windows\System32\drivers\etc\hosts -Force
Conclusion
Array operations are an essential part of the PowerShell, and while working with hundreds or thousands of the elements, we need to add or remove few items from the ArrayList or an array and in that case, we can use the array operations method to add the data.
Recommended Articles
This is a guide to PowerShell add to array. Here we discuss the introduction; how does PowerShell add to array works? and examples. You may also have a look at the following articles to learn more –