How to Join an Array into a String in PowerShell?

In this PowerShell tutorial, I will explain how to join an array into a string in PowerShell using different methods with examples.

To join an array into a string in PowerShell, you can use the -join operator. For example, $string = $array -join ‘,’ will concatenate all elements of $array into a single string, separated by commas. The Join-String cmdlet can also be used for this purpose, providing advanced options such as script block expressions for custom join operations.

Join an Array into a String in PowerShell

Let us say, I have an array in PowerShell like the below one.

$array = "apple", "banana", "cherry"

Now, let’s look at how we can turn this array into a single string in PowerShell.

1. Using the -join Operator

The simplest way to join an array into a string is by using the -join operator in PowerShell. This operator takes all elements of an array and concatenates them into a single string, with the option to add a delimiter.

Example 1: Joining Without a Delimiter

$array = "apple", "banana", "cherry"
$string = $array -join ""
Write-Output $string

Output:

applebananacherry

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

Join an Array into a String in PowerShell

Example 2: Joining With a Delimiter

$array = "apple", "banana", "cherry"
$string = $array -join ", "
Write-Output $string

Output:

apple, banana, cherry

2. Using the Join-String Cmdlet

PowerShell 7 introduced a cmdlet called Join-String, which provides more flexibility and functionality when joining array elements to a string in PowerShell.

Here is an example:

$array = "apple", "banana", "cherry"
$string = $array | Join-String -Separator ", "
Write-Output $string

Output:

apple, banana, cherry

The Join-String cmdlet is particularly useful when working with complex objects or when you need to apply custom formatting to each element before joining.

3. Using .NET Methods

PowerShell is built on top of the .NET Framework, and therefore, you can use .NET methods to accomplish the same task.

Example: Using String.Join()

$array = "apple", "banana", "cherry"
$string = [String]::Join(", ", $array)
Write-Output $string

Output:

apple, banana, cherry

This method is similar to the -join operator but can be useful if you are more familiar with .NET or are working within a .NET context.

4. Custom Functions for Joining Arrays

You can also create a custom function to join an Array into a String in PowerShell.

Example: Creating a Custom Join Function

function Join-ArrayToString {
    param(
        [String[]]$Array,
        [String]$Delimiter = ""
    )

    return $Array -join $Delimiter
}

$array = "apple", "banana", "cherry"
$string = Join-ArrayToString -Array $array -Delimiter ", "
Write-Output $string

Output:

apple, banana, cherry

Creating a custom function allows you to encapsulate the logic for joining arrays and reuse it throughout your scripts with ease.

Conclusion

Joining an array into a string in PowerShell is possible using several different methods. Whether you prefer using the simple -join operator, the more advanced Join-String cmdlet, .NET methods, or writing your own custom function, PowerShell provides different methods.

In this PowerShell tutorial, I have explained how to Join an Array into a String in PowerShell using different methods like:

  • Using the -join Operator
  • Using the Join-String Cmdlet
  • Using .NET Methods
  • Custom Functions for Joining Arrays

You may also like: