EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell add to array
 

PowerShell add to array

Updated March 8, 2023

PowerShell add to array

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

PowerShell add to array 1

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:

PowerShell add to array 2

If we check here, they all are added at the specific index incremented by one.

Code:

$arr[0] $arr[1]

Output:

PowerShell add to array 3

There won’t be any value at index 2 because we haven’t added it yet.

Code:

$arr[2]

Output:

PowerShell add to array 4

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 add to array 5

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:

PowerShell add to array 6

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:

PowerShell add to array 7

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:

PowerShell add to array 8

If we check the type of this array list, it is System.Object.

Code:

$arrlist.GetType()

Output:

PowerShell add to array 9

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:

PowerShell add to array 10

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:

adding one more animal “Cow”

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:

Addition operator to merging

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:

Arraylist to add the value

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:

PowerShell add to array 14

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:

Using loop

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:

PowerShell add to array 16

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:

Using the Indexing method

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:

PowerShell add to array 18

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:

method is not suitable

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 –

  1. PowerShell Sleep
  2. PowerShell SubString
  3. PowerShell not like
  4. Else If in PowerShell

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW