How to Replace Carriage Returns in Strings Using PowerShell?

There will be times when you want to replace carriage returns in strings using PowerShell. In this tutorial, we will learn how to replace carriage returns in strings using PowerShell.

To replace a string containing a carriage return in PowerShell, use the -replace operator with the carriage return escape sequence `r. For example, $string -replace “r”, “replacement text”will replace all carriage returns in$stringwith “replacement text”. If you want to remove carriage returns entirely, use an empty string as the replacement:$string -replace “r”, “”.

Replace Carriage Returns in Strings Using PowerShell

A carriage return (CR) is a special character used to reset a device’s position to the beginning of a line of text. It is represented by the escape sequence \r. In Windows, a combination of carriage return and line feed (LF), represented by \r\n, is used to mark the end of a line.

To find carriage returns in a string, we can use the -match operator in PowerShell. Here’s an example of how to detect if a string contains a carriage return:

$string = "This is a line of text.`rThis is another line of text."
if ($string -match "`r") {
    Write-Host "Carriage return found!"
} else {
    Write-Host "No carriage return found."
}

To replace carriage returns in strings, we use the -replace operator in PowerShell. The syntax for the -replace operator is 'pattern', 'replacement', where pattern is the text you want to replace, and replacement is the text you want to replace it with.

Here’s an example of how to replace carriage returns with a space in PowerShell:

$string = "This is a line of text.`rThis is another line of text."
$modifiedString = $string -replace "`r", " "
Write-Host $modifiedString

This will output:

This is a line of text. This is another line of text.

YOu can see the output in the screenshot below:

Replace Carriage Returns in Strings Using PowerShell

If you want to replace carriage returns with a newline character to maintain the line separation, you can do this:

$string = "This is a line of text.`rThis is another line of text."
$modifiedString = $string -replace "`r", "`n"
Write-Host $modifiedString

Check out How to Split a String by Length in PowerShell?

Replace Carriage Return in PowerShell Example

Now, let me show you a real example of how to replace carriage return in PowerShell.

Suppose you have a CSV file, but the data contains unwanted carriage returns.

Here’s how to clean up the CSV file using PowerShell:

$csvFilePath = "C:\Data\Customers.csv"
$cleanedFilePath = "C:\Data\Customers_Cleaned.csv"

$csvData = Get-Content $csvFilePath | ForEach-Object { $_ -replace "`r", "" }
$csvData | Set-Content $cleanedFilePath

In this example:

  1. We specify the path to the original CSV file ($csvFilePath) and the desired path for the cleaned CSV file ($cleanedFilePath).
  2. We use Get-Content to read the contents of the CSV file and pipe it to ForEach-Object.
  3. Inside ForEach-Object, we apply the -replace operator to remove carriage returns from each line of the CSV data.
  4. Finally, we use Set-Content to write the cleaned data to a new CSV file.

Check out PowerShell Substring() Example

PowerShell Remove Carriage Return

Sometimes, you may want to remove carriage returns altogether. Here’s how you can do it:

$string = "This is a line of text.`rThis is another line of text."
$modifiedString = $string -replace "`r", ""
Write-Host $modifiedString

This will output:

This is a line of text.This is another line of text.

Check out How to Convert String to Integer in PowerShell?

Remove CRLF From String in PowerShell

Let me show you an example of removing carriage return and line feed (CRLF) characters from a string using PowerShell.

Suppose you have a string containing customer information, including unwanted CRLF characters. Here’s an example:

$customerInfo = "John Doe`r`n123 Main St.`r`nNew York, NY 10001`r`njohn.doe@example.com"

In this string, the \r\n sequences represent the CRLF characters, which introduce line breaks. To remove these characters and obtain a clean, single-line string, you can use the -replace operator in PowerShell

$customerInfo = "John Doe`r`n123 Main St.`r`nNew York, NY 10001`r`njohn.doe@example.com"
$cleanedInfo = $customerInfo -replace "`r`n", ""
Write-Output $cleanedInfo

In this code:

  1. We use the -replace operator to replace all occurrences of \r\n with an empty string ("").
  2. The resulting string, without the CRLF characters, is stored in the $cleanedInfo variable.
  3. Finally, we use Write-Output to display the cleaned string.

The output will be:

John Doe123 Main St.New York, NY 10001john.doe@example.com

As you can see, the CRLF characters have been removed, and the customer information is now a single-line string.

Here is the exact output in the screenshot below:

Remove CRLF From String in PowerShell

This technique is useful when dealing with data that includes unwanted line breaks, such as when processing CSV files or handling user input.

Here’s another example that explains removing CRLF characters from a CSV file:

$csvFilePath = "C:\Data\Customers.csv"
$cleanedFilePath = "C:\Data\Customers_Cleaned.csv"

$csvData = Get-Content $csvFilePath | ForEach-Object { $_ -replace "`r`n", "" }
$csvData | Set-Content $cleanedFilePath

In this example:

  1. We specify the path to the original CSV file ($csvFilePath) and the desired path for the cleaned CSV file ($cleanedFilePath).
  2. We use Get-Content to read the contents of the CSV file and pipe it to ForEach-Object.
  3. Inside ForEach-Object, we apply the -replace operator to remove CRLF characters from each line of the CSV data.
  4. Finally, we use Set-Content to write the cleaned data to a new CSV file.

Conclusion

In this PowerShell tutorial, I will explain how to replace carriage returns in strings using PowerShell.

You may also like:

>
Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 120 Page FREE PDF on Microsoft Power Platform Tutorial. Learn Now…