In one of the tutorials, I explained how to concatenate strings in PowerShell. Here, I will show you an advanced concept. I will explain how to concatenate strings inside loops in PowerShell using various methods. I will show how to do this in for loop, foreach loop, etc.
String concatenation is the process of joining two or more strings together on PowerShell. PowerShell provides multiple ways to concatenate strings within loops, including the + operator, the += operator, the Join method, and the StringBuilder class, etc.
Concatenate Strings Inside Loops in PowerShell
Now, let me explain each method using examples.
1. Using the + Operator
The + operator is the easiest way to concatenate strings in PowerShell. However, using this cmdlet in loops will slightly impact performance.
Example: for Loop
Here is how to use the + operator to concatenate strings inside a for loop in PowerShell.
$combinedString = ""
for ($i = 1; $i -le 1000; $i++) {
$combinedString = $combinedString + "Number $i "
}
Write-Output $combinedStringI executed the above script, and you can see the output in the screenshot below:

Example: foreach Loop
Below is the complete script to concatenate strings inside a PowerShell foreach loop.
$combinedString = ""
foreach ($i in 1..1000) {
$combinedString = $combinedString + "Number $i "
}
Write-Output $combinedStringIn both examples, each iteration creates a new string, which can be inefficient for large loops.
2. Using the += Operator
Now, let me tell you the 2nd approach: using the += operator to concatenate strings inside a loop in PowerShell.
The += operator appends the right-hand operand to the left-hand operand, making it a more concise option than the + operator. However, it still suffers from performance issues in large loops.
Example: for Loop
Here is the complete script to concatenate strings inside a for loop using the += Operator in PowerShell.
$combinedString = ""
for ($i = 1; $i -le 1000; $i++) {
$combinedString += "Number $i "
}
Write-Output $combinedStringExample: foreach Loop
Here is the complete script to concatenate strings inside a foreach loop using the += Operator in PowerShell.
$combinedString = ""
foreach ($i in 1..1000) {
$combinedString += "Number $i "
}
Write-Output $combinedStringThe += operator also creates a new string for each iteration, leading to performance drawbacks similar to those of the + operator in PowerShell.
After I executed the above PowerShell script using VS code, you can see the output in the screenshot below:

Read Concatenate Strings with New Line in PowerShell
3. Using the Join() Method
The Join method is more efficient for concatenating strings when dealing with arrays or collections. It processes the entire array at once, reducing the overhead of multiple string creations.
Now, let me show you an example of how to use the join() method to concatenate strings in a PowerShell foreach loop.
Example: foreach Loop with Join
$numbers = 1..1000 | ForEach-Object { "Number $_" }
$combinedString = [string]::Join(" ", $numbers)
Write-Output $combinedStringThis method is efficient because it constructs the final string in a single operation, but it requires creating an array of strings first.
4. Using StringBuilder
The StringBuilder class is designed for scenarios where you need to modify a string multiple times, making it the most efficient method for large loops.
Example: for Loop with StringBuilder
Now, let me tell you an efficient way that will be very helpful in larger loops.
And that is to use the StringBuilder class.
Below is an example of how to use StringBuilder to concatenate strings in PowerShell within a for loop.
$stringBuilder = New-Object System.Text.StringBuilder
for ($i = 1; $i -le 1000; $i++) {
[void]$stringBuilder.Append("Number $i ")
}
$combinedString = $stringBuilder.ToString()
Write-Output $combinedStringExample: foreach Loop with StringBuilder
Below is an example of how to use StringBuilder to concatenate strings in PowerShell within a foreach loop.
$stringBuilder = New-Object System.Text.StringBuilder
foreach ($i in 1..1000) {
[void]$stringBuilder.Append("Number $i ")
}
$combinedString = $stringBuilder.ToString()
Write-Output $combinedStringThe StringBuilder class minimizes the number of intermediate strings created during concatenation, making it highly efficient for large loops.
Conclusion
PowerShell provides multiple ways to concatenate strings within loops. If you are working with small loops, you can use the + or += operators. However, for larger loops, you can use the Join method or the StringBuilder class to concatenate strings inside loops in PowerShell.
- Simple Concatenation (
+Operator): Easy to understand but inefficient for large loops. +=Operator: Convenient but can lead to performance issues for large loops.JoinMethod: Efficient for concatenating arrays or collections.StringBuilder: Most efficient for large loops and performance-critical scenarios.
I hope now you can concatenate strings inside various loops in PowerShell using the above methods.
You may also like the following tutorials:
- How to Trim Strings in PowerShell?
- Convert Strings to Lowercase or Uppercase in PowerShell
- Count the Number of Characters in a String in PowerShell
- Concatenate String and Variable 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.