How to Join Arrays in PowerShell?

Do you want to combine multiple arrays in PowerShell? In this PowerShell tutorial, I will explain how to join arrays in PowerShell, a common task when combining multiple arrays into a single array or a string.

To join arrays in PowerShell, you can use the + operator to concatenate them. For instance, $joinedArray = $array1 + $array2 will create a new array $joinedArray containing all elements from $array1 followed by all elements from $array2. Alternatively, you can use the += operator to append one array to another: $array1 += $array2. This modifies $array1 to include the elements of $array2.

Join Arrays in PowerShell

An array is a data structure that can hold multiple values, usually of the same type. In PowerShell, arrays are incredibly flexible and contain different data types.

Here’s a simple example of an array in PowerShell:

$myArray = @(1, 2, 3, 4, 5)

The @() symbol is used to denote an array, and the elements are separated by commas.

Now, let us see how to join arrays in PowerShell.

Join PowerShell Arrays Using the + Operator

One of the simplest ways to join two arrays in PowerShell is by using the + operator. This operator allows you to concatenate arrays, effectively appending one array to the end of another.

Here’s an example:

$array1 = @(1, 2, 3)
$array2 = @(4, 5, 6)
$joinedArray = $array1 + $array2
$joinedArray

The $joinedArray will contain the elements (1, 2, 3, 4, 5, 6).

After I executed the script using Windows PowerShell ISE, you can see the output in the screenshot below.

Join Arrays in PowerShell

Joining PowerShell Arrays into a String Using -join

You can use the -join operator, if you want to join an array of strings into a single string. This is particularly useful when you have an array of text that you want to output as a single line or when you need to construct a single string from multiple elements.

Here’s how you can use the -join operator:

$stringArray = @('PowerShell', 'is', 'awesome')
$joinedString = $stringArray -join ' '

The $joinedString will now contain the text "PowerShell is awesome".

You can also specify a delimiter to be placed between each element:

$stringArray = @('PowerShell', 'is', 'awesome')
$joinedString = $stringArray -join ', '

Now, $joinedString will be "PowerShell, is, awesome".

Using Join-String Cmdlet

PowerShell 7 introduced a new cmdlet called Join-String, which is designed to join strings from pipeline objects into a single string. This cmdlet provides more flexibility, such as specifying a separator or a script block to control the format of the output.

Here’s an example using Join-String:

$stringArray = @('PowerShell', 'is', 'awesome')
$joinedString = $stringArray | Join-String -Separator ', '

This will produce the same output as the -join operator with a comma separator: "PowerShell, is, awesome".

Join PowerShell Array Elements with a Custom Function

Sometimes, you might need to join array elements in a more complex way. This is where custom functions can come in handy. You can create a function that takes an array as input and processes each element according to your requirements.

Here’s a simple custom function that joins an array of numbers into a string in PowerShell, with each number increased by one:

function Join-ArrayWithIncrement {
    param([int[]]$numbers)
    # Increment each number in the array by 1
    $output = $numbers | ForEach-Object { $_ + 1 }
    # Join the incremented numbers into a single string with ', ' as separator
    return $output -join ', '
}

$numberArray = @(1, 2, 3)
$joinedNumbers = Join-ArrayWithIncrement -numbers $numberArray
$joinedNumbers

The $joinedNumbers will contain "2, 3, 4".

You can see the output after I ran the code using Windows PowerShell ISE.

PowerShell join arrays

Conclusion

Joining arrays in PowerShell is a straightforward process, whether you’re concatenating them into a new array or combining their elements into a string. Using the + operator, the -join operator, or the Join-String cmdlet, you can easily merge arrays to suit your scripting needs. With custom functions, you have the full power of PowerShell to manipulate and join arrays in any way you require.

In this PowerShell tutorial, I have explained how to join arrays in PowerShell using different methods like:

  • using + operator
  • using -join operator
  • using Join-String Cmdlet
  • using Custom functions

You may also like: