How to Pass Arrays to Functions in PowerShell?

Do you need to pass an array to a PowerShell function? In this PowerShell tutorial, I will explain how to pass arrays to functions in PowerShell with examples.

To pass an array to a function in PowerShell, define the function parameter to accept an array type, and then call the function with the array as an argument. For example, function Get-Data([array]$items) { … } defines a function that accepts an array, and Get-Data $myArray passes the array to the function.

Declare Functions in PowerShell

In PowerShell, you can declare a function using the function keyword, followed by the name of the function and a pair of curly braces {} containing the code block.

function MyFunction {
    # Code goes here
}

Now, let us see, how to pass an array to a PowerShell function.

Pass Arrays to Functions in PowerShell

Passing an array to a function in PowerShell is straightforward. You define a parameter in the function, and when calling the function, you pass the array to it.

Method 1: Direct Passing

Here’s an example of how to pass an array directly to a function in PowerShell:

function Process-Array ($array) {
    foreach ($item in $array) {
        Write-Host "Processing item: $item"
    }
}

# Define an array
$numbers = 1..5

# Pass the array to the function
Process-Array -array $numbers

In this example, $numbers is an array of integers from 1 to 5. The Process-Array function takes one parameter, $array, and processes each item in the array with a foreach loop.

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

How to Pass Arrays to Functions in PowerShell

Method 2: Using Splatting

Splatting is a method of passing parameters to functions where you use a hashtable or array to hold the parameter values. Here’s how to use splatting with an array in PowerShell:

function Get-Total ($numbers) {
    $sum = 0
    foreach ($number in $numbers) {
        $sum += $number
    }
    return $sum
}

# Define an array
$numbers = 1..5

# Splatting the array to the function
$total = Get-Total -numbers $numbers

Write-Host "The total is: $total"

In this example, the Get-Total function calculates the sum of the numbers in the array. Splatting isn’t different from direct passing in this case, but it’s useful when you have many parameters.

Method 3: Passing by Reference

You can also pass an array by reference to a PowerShell function, which allows the function to modify the original array.

Here is a complete example of how to pass an array to a function in PowerShell.

function Add-Element ([ref]$array, $newElement) {
    $array.Value += $newElement
}

# Define an array
$numbers = @(1, 2, 3)

# Pass the array by reference to the function
Add-Element ([ref]$numbers) 4

Write-Host "Array after adding element: $numbers"

When you run this script, the Add-Element function adds a new element to the original array, and you can see the updated array with the new element added. Once you execute the script, you can see the output in the below screenshot.

PowerShell Pass Arrays to Functions

Tips for Working with Arrays in Functions

  • When defining a function parameter for an array, you can explicitly declare it as an array type using [array] or [type[]], where type is the specific data type of the array elements.
  • Use meaningful names for your function parameters to make your code more readable.
  • Remember that arrays in PowerShell are zero-indexed, meaning that the first element is at index 0.

Conclusion

There are different ways to pass an array to a function in PowerShell. In this PowerShell tutorial, I have explained how to pass arrays to function in PowerShell using the below methods:

  • Direct Passing
  • Using Splatting
  • Passing by Reference

You may like the following tutorials: