Home » PowerShell » PowerShell Replace Newline with comma

PowerShell Replace Newline with comma

In PowerShell `n is used to add a new line. To replace the new line with a comma in PowerShell, check if it contains the new line using `r`n and uses PowerShell replace() method to replace the new line with a comma.

In this article, we will discuss how to use PowerShell replace() method to replace the new line with a comma in a string and use PowerShell replace operator to replace a new line in with a comma in a PowerShell string.

PowerShell Replace Newline with Comma using replace() method

Use the PowerShell replace() method to replace the new line with a comma. replace() takes two arguments; string to find and string to replace with found text in PowerShell string.

Let’s say, you have a string that contains a multiline string. Use the `r`n character (CRLF) to check if the string contains carriage returns and a new line character, it returns true if the string contains a new line.

To replace the newline with a comma using the PowerShell replace() method, refer to the following code.

$str = "Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.
Refer the link https://shellgeek.com/tag/active-directory-2/"

# Check if the string contains carriage return
$str.Contains("`r")   
True

# Check if the string contains new line
$str.Contains("`n")

# Replace newline with comma
$str.Replace("`r`n",",")
 

In the above PowerShell script, the $str variable stores a multi-line string. We use `r and `n to check if the string contains a carriage return and a new line.

Call Replace() method over the $str to replace the new line with a comma in the given PowerShell string.

It takes `r`n as the first argument to search the string for a new line and uses commas as the second argument to replace with the found text.

The output of the above PowerShell script to replace newline with comma using replace() method is:

PowerShell Replace Newline with Comma
PowerShell Replace Newline with Comma

Cool Tip: How to replace with wildcard in PowerShell!

Replace Newline with Comma using PowerShell replace Operator

Using the PowerShell replace operator, it can replace the new line with a comma.

Let’s consider the above variable $str which contains a multiline string.

 $str = "Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.
Refer the link https://shellgeek.com/tag/active-directory-2/"  

# Replace newline with comma using replace operator
$str -replace '\r\n',','

In the above PowerShell script, we have used PowerShell replace operator over the $str variable to check for carriage return and new line using \r\n and replace with comma.

Using the PowerShell replace operator, you will need to use \r\n to check for CRLF.

The output of the above PowerShell script to replace a new line with a comma is:

PS C:\> $str
Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.
Refer the link https://shellgeek.com/tag/active-directory-2/

PS C:\> $str -replace '\r\n',','
Hello SysAdmin, Welcome to PowerShell Programming.,Let's begin your Active Directory management using the PowerShell journey 
here.,Refer the link https://shellgeek.com/tag/active-directory-2/

PS C:\> 

Conclusion

I hope the above article on how to replace newline with comma using the PowerShell replace() method or PowerShell replace operator is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.