How to Convert an Array to a String in PowerShell?

Do you need to convert an array of elements into a single string in PowerShell? In this PowerShell tutorial, I will explain how to convert an array to a string in PowerShell. We’ll explore several methods to convert an array to a string in PowerShell, with complete examples.

To convert an array to a string in PowerShell, you can use the -join operator to concatenate the array elements with a specified delimiter. For example, $string = $array -join ', ' will create a string from the array $array, with each element separated by a comma and a space. Alternatively, you can pipe the array to Out-String for a simple conversion without delimiters.

Convert an Array to a String in PowerShell

An array is a data structure that holds multiple items, which can be of various types, including strings, integers, and even other arrays. In PowerShell, arrays are defined using the @() syntax, and elements within an array are separated by commas.

For example:

$array = @(1, 2, 'three', 'four')

Let’s look at different ways to convert this array into a string in PowerShell.

Method 1: Using the -join Operator

The -join operator is one of the simplest ways to convert an array to a string in PowerShell. It concatenates each array element into a single string, with the option to add a delimiter between elements.

Example:

$array = @(1, 2, 'three', 'four')
$string = $array -join ', '
Write-Output $string

Output:

1, 2, three, four

In this example, we used a comma followed by a space as the delimiter, but you can use any string or character as the delimiter, or even an empty string if you don’t want any separator.

Here, you can see the output in the screenshot below, after I executed the PowerShell script.

Convert an Array to a String in PowerShell

Method 2: Using Out-String Cmdlet

The Out-String cmdlet in PowerShell is another way to convert an array to a string. It’s particularly useful when you want to convert the default output of an array to a string format.

Example:

$array = @(1, 2, 'three', 'four')
$string = $array | Out-String
Write-Output $string.Trim()

The Trim() method in PowerShell is used here to remove any extra whitespace from the beginning and end of the string, which can be a common occurrence when using Out-String.

Method 3: Using .NET’s String.Join Method

PowerShell is built on .NET, so you can leverage .NET classes and their methods. One such method is String.Join, which is similar to the -join operator but is a static method of the String class.

Example:

$array = @(1, 2, 'three', 'four')
$string = [System.String]::Join(", ", $array)
Write-Output $string

This method is particularly powerful because it provides the flexibility of .NET’s string manipulation capabilities.

Method 4: Using Type Casting

You can also convert an array to a string by casting the array as a string type in PowerShell. This method is known as explicit type conversion or casting.

Example:

$array = @(1, 2, 'three', 'four')
$string = [string]$array
Write-Output $string

This will convert the array into a string with space as the default delimiter between elements in PowerShell.

Method 5: Using ForEach-Object and += Operator

You can iterate through each element of the array and append it to a string variable using the += operator in PowerShell. This method provides more control over the conversion process.

Example:

$array = @(1, 2, 'three', 'four')
$string = ''
$array | ForEach-Object { $string += "$_ " }
Write-Output $string.Trim()

This script concatenates each element of the array to the $string variable with a space in between. The Trim() method is used at the end to remove the trailing space in PowerShell.

Conclusion

Converting an array to a string in PowerShell is a common task that can be accomplished in several ways. Whether you prefer using built-in cmdlets like Out-String, operators like -join, .NET methods like String.Join, or even a simple loop with ForEach-Object, PowerShell provides the flexibility to handle this conversion to suit your needs.

In this PowerShell tutorial, I have explained how to convert an array to a string in PowerShell using various methods.

You may also like: