Pass an Array to a Function in PowerShell

Migel Hewage Nimesha Aug 24, 2022
  1. PowerShell Arrays
  2. Pass an Array to a PowerShell Function
Pass an Array to a Function in PowerShell

This article will focus on arrays and the convention of passing an array to a function in PowerShell.

PowerShell Arrays

PowerShell arrays are no different than those arrays in general purpose programming languages such as Java, Python, C#, etc. It can hold fixed-size primitive values or objects of any data type.

Syntax:

$intTypeArray = 34, 100, 1000, 45, 455, 1

To check the type of the variable $intTypeArray in the following example, we will use the in-built GetType() method.

check the type of the variable 1

As expected, the base type is System.Array. Since we have not explicitly specified the data type of this array, the PowerShell engine has created it as an array of objects.

It is possible to hold different types of elements in a single array, as shown in the following code.

$mixedElementArray = 200, 'stringElement', 12.555, 'hello'

Output:

check the type of the variable 2

As you can see, the default PowerShell array is based on the Object[] type.

In PowerShell, every value or object is inherited from the Object. So, any value or object is assignable to a default PowerShell array.

There is another variant of PowerShell arrays called strongly typed arrays. Arrays created as strongly typed can contain only a specific type of collection of elements.

When we create a strongly typed array, it is necessary to cast the reference variable to a specific array type such as int32[], string[], etc.

[string[]]$stringTypeArray = 'tesla', 'mecedes', 'audi', 'lambo'

Let’s check the type of $stringTypeArray.

$stringTypeArray.GetType()

Output:

check the type of the variable 3

Pass an Array to a PowerShell Function

Strongly typed arrays are recommended to use in your PowerShell programs because it has type safety. Whenever you need to pass an already defined array to a function, the following syntax should work properly.

function <function_identifier>([<data_type>[]]$<parameter_name>)
{

}

In this way, you can easily pass an array to function. Let’s first define a PowerShell function called letsPassAnArray.

function letsPassAnArray([string[]]$stringList) {
     foreach ($arrEle in $stringList)
    {
        Write-Host $arrEle
    }
}

In this case, we are passing a string-type array and printing each element inside the array using the foreach operator.

Next, we are going to call this function from the PowerShell script. Make sure to create a string-type array as well.

[string[]]$stringArr = 'Apple', 'Orange', 'Grapes'
letsPassAnArray($stringArr)

Output:

Pass an Array to a PowerShell Function

Migel Hewage Nimesha avatar Migel Hewage Nimesha avatar

Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.

Related Article - PowerShell Array