How to Remove Newline from String in PowerShell?

Today, a team member asked me about removing newlines from a string in PowerShell. I suggested a few methods. In this tutorial, I will show you how to remove newline from a string in PowerShell with some practical examples.

To remove newline characters from a string in PowerShell, you can use the -replace operator. This method handles both \n (line feed) and \r\n (carriage return and line feed) characters. For example, if you have a string $logContent = "Error occurred at line 1nError occurred at line 2rnError occurred at line 3″, you can clean it by running $cleanedContent = $logContent -replace “rn”, ” ” -replace “n", " " -replace "r”, ” “`. This will replace all newline characters with spaces, resulting in a single-line string.

Remove Newline from String in PowerShell

Newline characters can appear as \n (line feed) or \r\n (carriage return and line feed) in PowerShell. Let us check these methods.

Method 1: Using the -replace Operator

The -replace operator is used to remove newline characters from a string in PowerShell. This method can handle both \n and \r\n characters.

Let me show you an example.

Example:

Suppose you have log file content stored in a string, and you want to process it as a single line by removing all newline characters.

$logContent = "Error occurred at line 1`nError occurred at line 2`r`nError occurred at line 3"
$cleanedContent = $logContent -replace "`r`n", " " -replace "`n", " " -replace "`r", " "
Write-Output $cleanedContent

In this example, we first replace \r\n with a space, then \n, and finally \r. This ensures that all newline characters are removed, regardless of their format.

Here is the output you can see in the screenshot below:

powershell remove newline from string

Check out Check if a String Contains a Space in PowerShell

Method 2: Using the -join Method

Another effective method is to split the string by newline characters and then join the resulting array back into a single string. This way, we can remove the new line from a string in PowerShell.

Example:

Consider a CSV file content stored in a string; we want to remove newlines to prepare it for further processing.

$csvContent = "Name, Age`nJohn Doe, 30`nJane Smith, 25"
$cleanedContent = [string]::Join(" ", $csvContent.Split("`n"))
Write-Output $cleanedContent

In this example, we split the string by \n and then join the array elements with a space. This method is simple and effective for handling \n characters.

Here is the output in the screenshot below:

powershell remove newline from a string

Check out Generate Random Strings in PowerShell

Method 3: Using Out-String with -NoNewline Parameter

The Out-String cmdlet with the -NoNewline parameter can be used to remove newlines from the output generated by the PowerShell formatter. However, it preserves newlines that are part of the string objects themselves.

Here is an example.

Example:

Suppose you are generating a report and want to ensure that the output does not contain newlines.

$report = @"
Line 1
Line 2
Line 3
"@
$cleanedReport = $report | Out-String -NoNewline
Write-Output $cleanedReport

This method is useful when you want to format the output of complex objects without introducing additional newlines.

I executed the above script, and you can see the output in the screenshot below:

remove newline from string in powershell

Read Increment a String Variable by 1 in PowerShell

Remove Newline from String in PowerShell – Real-World Example: Cleaning Up JSON Data

Now, let me show you a real example of removing a newline from a string in PowerShell.

Suppose you have JSON data with newline characters, and you need to remove them for further processing.

$jsonData = @"
{
    "name": "John Doe",
    "age": 30,
    "address": {
        "street": "123 Main St",
        "city": "Anytown"
    }
}
"@

# Remove newlines
$cleanedJson = $jsonData -replace "`r`n", " " -replace "`n", " " -replace "`r", " "
Write-Output $cleanedJson

Removing the newlines allows you to manipulate the JSON data as a single string easily.

Conclusion

In this tutorial, I have explained how to remove newline characters from strings in PowerShell using different methods with real examples. For example, I have shown you can use the -replace operator, the -join method, or the Out-String cmdlet, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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