How to Add Value to an Array in a Function in PowerShell?

In one of my previous tutorials, I explained how to add values to a PowerShell array using various methods. But today, I will show you an advanced method. In this tutorial, I will explain how to add values to an array in a PowerShell function with examples.

To add a value to an array within a function in PowerShell using the += operator, you can define a function that takes the array and the new value as parameters. Inside the function, use the += operator to append the new value to the array and then return the updated array. Here’s a quick example:

function Add-Name {
    param (
        [string[]]$names,
        [string]$newName
    )

    $names += $newName
    return $names
}

# Example usage
$namesArray = @("John", "Jane", "Emily")
$namesArray = Add-Name -names $namesArray -newName "Michael"
Write-Output $namesArray

Add Value to an Array in a Function in PowerShell

Arrays in PowerShell allow you to store multiple items in a single variable. This is particularly useful when you need to handle lists of data, such as user names, file paths, or even configuration settings.

Before diving into functions, here is the basic syntax for creating and manipulating arrays in PowerShell:

# Creating an array
$names = @("John", "Jane", "Emily")

# Adding a value to an array
$names += "Michael"

In the example above, we create an array $names with three initial elements and then add a fourth element, “Michael”, using the += operator.

Now, let me show you different methods for adding value to an array in a function in PowerShell. The approach is similar when working within a function but with some additional considerations for scope and return values.

Method 1: Using the += Operator

The += operator is straightforward and easy to use. However, it’s important to note that this operator creates a new array each time it’s used, which can impact performance for large datasets.

Here is the complete PowerShell script.

function Add-Name {
    param (
        [string[]]$names,
        [string]$newName
    )

    $names += $newName
    return $names
}

# Example usage
$namesArray = @("John", "Jane", "Emily")
$namesArray = Add-Name -names $namesArray -newName "Michael"
Write-Output $namesArray

I executed the above PowerShell script, and you can see the output in the screenshot below:

add value to array in function in powershell

Check out Add an Element to the Beginning of an Array in PowerShell

Method 2: Using the ArrayList Class

For better performance, especially with large arrays, you can use the System.Collections.ArrayList class. This class allows dynamic resizing without creating new arrays.

function Add-Name {
    param (
        [System.Collections.ArrayList]$names,
        [string]$newName
    )

    $names.Add($newName) | Out-Null
    return $names
}

# Example usage
$namesArray = [System.Collections.ArrayList]@("John", "Jane", "Emily")
$namesArray = Add-Name -names $namesArray -newName "Michael"
Write-Output $namesArray

Here is the exact output in the screenshot below:

powershell add value to array in function

Method 3: Using the Add Method for Strongly-Typed Arrays

If you prefer to work with strongly-typed arrays, you can use the Add() method. This approach is less common but can be useful in certain scenarios.

function Add-Name {
    param (
        [string[]]$names,
        [string]$newName
    )

    $newArray = New-Object string[] ($names.Length + 1)
    [array]::Copy($names, $newArray, $names.Length)
    $newArray[$names.Length] = $newName
    return $newArray
}

# Example usage
$namesArray = @("John", "Jane", "Emily")
$namesArray = Add-Name -names $namesArray -newName "Michael"
Write-Output $namesArray

Conclusion

In this tutorial, I explained how to add values to an array within a function in PowerShell using various methods. The += operator is simple and easy to use, while the System.Collections.ArrayList class offers better performance for larger datasets. For those needing strongly-typed arrays, the Add method is also an option. I hope this helps you!

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.