Recently I got one requirement from one of my clients to concatenate a string with new line. I tried it using different methods using PowerShell. In this tutorial, I will show you how to concatenate strings with new line in PowerShell using various methods.
To concatenate strings with a new line in PowerShell, you can use several methods depending on your needs. The simplest approach is to use the + operator with the newline character `n, like $combinedString = $string1 + “n” + $string2. You can also embed variables directly within double quotes, such as $combinedString = “$string1n$string2”. The -join operator is useful for joining multiple strings, as in $combinedString = $strings -join “n”`.
Method 1: Using the + Operator
The + operator can be used to concatenate strings in PowerShell. To add a new line, you can use the special character `n.
Here is an example of using the character `n with the + operator in PowerShell to concatenate strings with a new line.
$part1 = "This is the first part of the string."
$part2 = "This is the second part of the string."
# Concatenate with a new line
$combinedString = $part1 + "`n" + $part2
Write-Output $combinedStringExplanation:
$part1and$part2are two separate strings.- The
+operator concatenates these strings. - The special character
`nis used to insert a new line between the strings.
I executed the above PowerShell script using VS code and you can see the exact output in the screenshot below:

Read Concatenate String and Integer in PowerShell
Method 2: Using Double Quotes
Let me tell you another method in PowerShell to concatenate strings with newlines in PowerShell.
You can use double quotes to concatenate strings and include new lines directly within the quotes.
$part1 = "This is the first part of the string."
$part2 = "This is the second part of the string."
# Concatenate with a new line
$combinedString = "$part1`n$part2"
Write-Output $combinedStringExplanation:
- In PowerShell, double-quoted strings allow for variable interpolation.
- The
`ncharacter is used within the double quotes to insert a new line.
Here is the output in the screenshot below, after I executed the above PowerShell script using VS code.

Method 3: Using -join Operator
In PowerShell, you can also use the -join operator to concatenate an array of strings with a specified separator, which can be a new line.
Here is the complete script to concatenate a string with new line using PowerShell.
$strings = @("This is the first part of the string.", "This is the second part of the string.")
# Concatenate with a new line
$combinedString = $strings -join "`n"
Write-Output $combinedStringExplanation:
$stringsis an array containing multiple strings.- The
-joinoperator concatenates the elements of the array, using`nas the separator.
You can check out the output in the screenshot below after I executed the above script in my local system.

Read Concatenate Strings Inside Loops in PowerShell
Method 4: Using Add-Content for File Operations
Now, let me show you another method of concatenating strings with new lines and that is Add-Content. If you need to append strings to a file with new lines, Add-Content can be used in PowerShell.
$part1 = "This is the first part of the string."
$part2 = "This is the second part of the string."
$filePath = "C://MyFolder/output.txt"
# Write initial content
Set-Content -Path $filePath -Value $part1
# Append with a new line
Add-Content -Path $filePath -Value "`n$part2"Explanation:
Set-Contentwrites the initial content to the file.Add-Contentappends additional content, including a new line, to the file.
Now, when I open the specified file, you can see the exact output. Here is the screenshot below for your reference.

Method 5: Using StringBuilder
For large strings concatenation in PowerShell, you can use System.Text.StringBuilder. Here is an example of how to use StringBuilder to concatenate strings with new lines in PowerShell.
Add-Type -TypeDefinition @"
using System.Text;
public class StringBuilderExample {
public static string ConcatenateWithNewLine(string part1, string part2) {
StringBuilder sb = new StringBuilder();
sb.Append(part1);
sb.AppendLine();
sb.Append(part2);
return sb.ToString();
}
}
"@
$part1 = "This is the first part of the string."
$part2 = "This is the second part of the string."
# Use the custom method to concatenate
$combinedString = [StringBuilderExample]::ConcatenateWithNewLine($part1, $part2)
Write-Output $combinedStringExplanation:
Add-Typeis used to define a new classStringBuilderExamplewith a method for concatenation.StringBuilderis used for efficient string manipulation.AppendLine()method adds a new line.
Conclusion
In this PowerShell tutorial, I have explained how to concatenate strings with new lines using various methods like below:
- Using the
+Operator: Simple and straightforward for basic concatenation needs. - Using Double Quotes: Allows for easy variable interpolation and inclusion of new lines.
- Using the
-joinOperator: Ideal for concatenating arrays of strings with a specified separator. - Using
Add-Contentfor File Operations: Useful for appending strings to files with new lines. - Using
StringBuilderfor Performance: Best for handling large-scale string concatenation efficiently.
If you still have questions, feel free to comment below.
You may also like:
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.