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.

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 the cmdlet in PowerShell is another way to convert an array to a string. It’s particularly useful when you want to convert the array’s default output 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.

Look at the screenshot below. This is the output after I executed the PowerShell script above using VS code.

powershell array to 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.

Here is the output; look at the screenshot below:

convert array to string powershell

This is another way to convert an array to a string 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.

join an array of strings in PowerShell

Here, I will show you one example of “PowerShell join array of strings”.

To join an array of strings in PowerShell, you can use the -join operator. Here is an example:

# Define an array of strings
$arrayOfStrings = @("Hello", "world", "from", "PowerShell")

# Join the array into a single string with spaces as the delimiter
$joinedString = $arrayOfStrings -join " "

# Output the result
Write-Output $joinedString

This will output:

Hello world from PowerShell

You can see the output in the screenshot below after I executed the above PowerShell using Visual Studio code.

powershell join array of strings

PowerShell: array to string with delimiter

To convert an array to a string with a specified delimiter in PowerShell, you can use the -join operator. Here is a detailed example:

  1. Define an Array: First, create an array of strings.
  2. Join the Array: Use the -join operator with the desired delimiter.
# Define an array of strings
$arrayOfStrings = @("apple", "banana", "cherry")

# Join the array into a single string with commas as the delimiter
$joinedString = $arrayOfStrings -join ", "

# Output the result
Write-Output $joinedString

Here is an explanation of the above PowerShell script.

  1. Array Definition: The array $arrayOfStrings contains three elements: "apple""banana", and "cherry".
  2. Join Operation: The -join operator is used to concatenate the elements of the array into a single string, with each element separated by a comma and a space (", ").
  3. Output: The Write-Output cmdlet prints the resulting string.

The output of the above script will be:

apple, banana, cherry

You can also see in the screenshot below:

powershell array to string with delimiter

Conclusion

Converting an array to a string in PowerShell is possible by using different ways like by using built-in cmdlets like Out-String, operators like -join, .NET methods like String.Join, or even a simple loop with ForEach-Object, etc.

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

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.