How do I remove all newlines from a string in PowerShell? How do I remove all newlines from a string in PowerShell? powershell powershell

How do I remove all newlines from a string in PowerShell?


Commenters point to the return `r and maybe that special character should be replaced separately. Guessing this could be done with the .replace() method and some regex. Or more simply (and clumsily I admit) with another variable before your $list1 += line, such as:

$bod = $message.body -replace "`n",", " -replace "`r",", "

Overkill, but here's a small example from scratch. I'm suggesting you add $y to make it easier to manipulate the message body.

$x = new-object -type psObject$x | add-member memburr1 -membertype noteproperty -value "no trouble here"$x | add-member memburr2 -membertype noteproperty -value "uh `n oh `r spaghettio"$y = $x.memburr2 -replace "`n",", " -replace "`r",", "$z = @()$z += "$($x.memburr1);$y"

If this doesn't help, I'd be curious what appears immediately before and after a problematic line break in the output.

EDIT: Or use the .replace() method twice:

$x.memburr2.replace("`n",", ").replace("`r",", ")


None of the above worked for me. What did work was:

$foo = [string]::join("",($foo.Split("`n")))