How to Add Items to an Array in PowerShell (5 Simple Methods)

Recently, I was developing an automation script to manage a large inventory of servers, and I needed to add items to an array as the script ran dynamically. The challenge is that PowerShell handles arrays differently than many other programming languages.

In this tutorial, I will show you five proven methods to add items to an array in PowerShell, based on my decade-plus experience working with PowerShell scripts.

Let’s dive in!

Method 1: Using the += Operator

The best way to add an item to an array is using the += operator in PowerShell. While simple, this approach creates a new array in memory each time.

# Initialize an empty array
$servers = @()

# Add items one by one
$servers += "USEAST-SQL001"
$servers += "USWEST-SQL002"
$servers += "USCENT-SQL003"

# Display the array
$servers

This will output:

USEAST-SQL001
USWEST-SQL002
USCENT-SQL003

While this method is simple, it’s important to understand that PowerShell creates a new array each time you use +=, copying all existing elements plus the new one. For small arrays, this isn’t a problem, but for large arrays or when adding many items in a loop, it can cause performance issues.

Here is the exact output in the screenshot below:

powershell add item to array

Check out PowerShell Array of Strings

Method 2: Using the ArrayList Class

For better performance, especially with larger collections, you can use the ArrayList class from .NET, which is designed for efficient modification:

# Create an ArrayList
$serverList = New-Object System.Collections.ArrayList

# Add items (note the [void] to suppress output of the index)
[void]$serverList.Add("USEAST-WEB001")
[void]$serverList.Add("USWEST-WEB002")
[void]$serverList.Add("USCENT-WEB003")

# Display the ArrayList
$serverList

Unlike regular PowerShell arrays, the ArrayList offers better performance because it’s designed to be modified. The [void] prefix suppresses the index number that would otherwise be displayed when adding items.

You can see the exact output in the screenshot below:

powershell insert item to array

Check out Check If File Modified In Last 24 Hours Using PowerShell

Method 3: Using Generic List

The generic List class from .NET provides type safety and excellent performance; below is the script to insert items to an array list.

# Create a generic List of strings
$employeeList = New-Object System.Collections.Generic.List[string]

# Add items
$employeeList.Add("John Smith")
$employeeList.Add("Sarah Johnson")
$employeeList.Add("Michael Davis")

# Display the list
$employeeList

A generic List is often the best choice when you know the data type in advance. It combines the performance benefits of ArrayList with type safety, helping prevent errors when working with collections of a specific type.

Check out Download a File from URL Using PowerShell

Method 4: Using Array Methods

PowerShell arrays have methods like System.Array::CreateInstance that can help when you need to resize an array:

# Initialize an array with a specific size
$departments = [System.Array]::CreateInstance([string], 3)
$departments[0] = "Sales"
$departments[1] = "Marketing"
$departments[2] = "IT"

# Create a new, larger array and copy the old elements
$newSize = $departments.Length + 1
$newDepartments = [System.Array]::CreateInstance([string], $newSize)
[System.Array]::Copy($departments, $newDepartments, $departments.Length)

# Add the new item
$newDepartments[$departments.Length] = "Finance"

# Update our reference
$departments = $newDepartments

# Display the array
$departments

This approach gives you more direct control over memory allocation but requires more code than other methods. It’s useful in specific scenarios where you need fine-grained control over array resizing.

Check out Create a Folder If Not Exists in PowerShell

Method 5: Using the Pipeline for Array Creation

A very PowerShell-centric approach is to use the pipeline to create and populate arrays:

# Create and populate an array using the pipeline
$states = @(
    "California"
    "Texas"
    "Florida"
)

# Add more items
$states = @($states; "New York"; "Illinois")

# Display the array
$states

This creates a clean, readable syntax for defining and extending arrays. The semicolons separate items being added to the existing array.

Here is the exact output in the screenshot below:

Add Items to an Array in PowerShell

Another pipeline approach is to use commands that output objects:

# Get processes and filter to create an array
$criticalProcesses = Get-Process | Where-Object { $_.CPU -gt 50 } | ForEach-Object { $_.Name }

# Add more processes conditionally
if (Get-Service -Name "ImportantService" -ErrorAction SilentlyContinue) {
    $criticalProcesses += "ImportantService"
}

# Display the array
$criticalProcesses

This approach leverages PowerShell’s pipeline to create arrays from command output, which is a very natural pattern in PowerShell scripting.

Check out Create a File in PowerShell if it Doesn’t Exist

Performance Considerations

When working with large arrays, performance becomes crucial. In my experience managing enterprise-scale automation, I’ve found these performance differences to be significant:

  1. The += operator is slowest for large arrays (avoid in loops)
  2. ArrayList and List offer the best performance for frequent modifications
  3. Pipeline-based approaches are readable but not optimized for performance-critical scenarios

Here’s a simple benchmark to demonstrate the difference:

# Test array addition performance
$count = 10000
$timer = [System.Diagnostics.Stopwatch]::StartNew()

# Array with +=
$array = @()
for ($i = 0; $i -lt $count; $i++) {
    $array += $i
}
$timer.Stop()
Write-Host "Array with += took: $($timer.ElapsedMilliseconds) ms"

# ArrayList
$timer.Restart()
$arrayList = New-Object System.Collections.ArrayList
for ($i = 0; $i -lt $count; $i++) {
    [void]$arrayList.Add($i)
}
$timer.Stop()
Write-Host "ArrayList took: $($timer.ElapsedMilliseconds) ms"

# List<T>
$timer.Restart()
$list = New-Object System.Collections.Generic.List[int]
for ($i = 0; $i -lt $count; $i++) {
    $list.Add($i)
}
$timer.Stop()
Write-Host "List<T> took: $($timer.ElapsedMilliseconds) ms"

Arrays in PowerShell are powerful tools for organizing and manipulating collections of data. While the += operator is the simplest way to add items to an array, using ArrayList or List provides significantly better performance for larger collections or frequent modifications.

For most of my professional scripting work, I use the generic List for its combination of type safety and performance. For quick scripts where performance isn’t critical, the simple += operator works just fine.

I hope you found this article helpful for your PowerShell scripting needs. If you have any questions or want to share your own array techniques, please leave them in the comments below.

Other PowerShell articles you may also like:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App