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 $stringOutput:
applebananacherryI executed the PowerShell script, and you can see the output in the screenshot below:

Example 2: Joining With a Delimiter
$array = "apple", "banana", "cherry"
$string = $array -join ", "
Write-Output $stringOutput:
apple, banana, cherry2. 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 $stringOutput:
apple, banana, cherryThe 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 $stringOutput:
apple, banana, cherryThis 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 $stringOutput:
apple, banana, cherryCreating 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:
- How to Reverse An Array in PowerShell?
- How To Split Path Into Array In PowerShell?
- How to Find the Highest Number in an Array in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.