Concatenate String with NewLine in PowerShell

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 $combinedString

Explanation:

  • $part1 and $part2 are two separate strings.
  • The + operator concatenates these strings.
  • The special character `n is 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:

powershell concatenate string with new line

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 $combinedString

Explanation:

  • In PowerShell, double-quoted strings allow for variable interpolation.
  • The `n character 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.

Concatenate Strings with New Line in PowerShell

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 $combinedString

Explanation:

  • $strings is an array containing multiple strings.
  • The -join operator concatenates the elements of the array, using `n as the separator.

You can check out the output in the screenshot below after I executed the above script in my local system.

How to Concatenate Strings with New Line in PowerShell

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-Content writes the initial content to the file.
  • Add-Content appends 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.

powershell concatenate string with new line using Add-Content

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 $combinedString

Explanation:

  • Add-Type is used to define a new class StringBuilderExample with a method for concatenation.
  • StringBuilder is 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:

  1. Using the + Operator: Simple and straightforward for basic concatenation needs.
  2. Using Double Quotes: Allows for easy variable interpolation and inclusion of new lines.
  3. Using the -join Operator: Ideal for concatenating arrays of strings with a specified separator.
  4. Using Add-Content for File Operations: Useful for appending strings to files with new lines.
  5. Using StringBuilder for Performance: Best for handling large-scale string concatenation efficiently.

If you still have questions, feel free to comment below.

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.