Author Posts

April 3, 2014 at 8:11 am

Trying to replace CRLF in a small PowerShell script

Works OK with standard characters bit not with `n ;

file.txt look like this:

Line1
Line2
Line3
Line4

would like the file to look like this:

Line1,Line2,Line3,Line4

Here is the script below

$Strings = Get-Content e:\powershell\file.txt

Foreach ($String in $Strings){

$string -replace ("`n" , ",")
}

April 3, 2014 at 8:13 am

Get-Content returns an array of strings, by default, and those strings will not include the actual CR/LF characters that were present in the file. All you need to do is use the -join operator to combine the array back into a single comma-separated string:

$String = (Get-Content e:\powershell\file.txt) -join ','