I’m using Export-CSV but the resulting file is terminated with CRLF. What’s the method to change this to LF?
Thanks.
I’m using Export-CSV but the resulting file is terminated with CRLF. What’s the method to change this to LF?
Thanks.
That’s the default on Windows.
Whatfor do you need this? What’s the actual issue you’re trying to solve?
The source file (processed by Import-Csv) is terminated with LF and the downstream system which processes the output from Export-Csv needs the file to be terminated with LF. There is processing between the import and export but the line-ending needs to be retained.
You may treat the file separately after the Export-Csv happend.
… have you tried PowerShell v 7.x?
$content = Get-Content -Path 'File.CSV' -Raw
$Replaced = $content -replace "`r`n", "`n"
Set-Content -Path 'File.CSV' -Value $Replaced
Seems to do the trick if needed to do programmatically. I took a CRLF csv (As reported by vs code), ran it through the code above, and it now reports as LF. Some googling around suggests many others doing the above.
This post is related to another and Import-Csv solves the problem of embedded linefeeds within a field. So Import-Csv needs to remain part of the solution. Export-Csv is then a flexible way to recombine the fields into their original state.
I was hoping there was something I’d missed in Export-Csv to change the line endings (-Encoding, etc.)
You can specify the encoding with the parameter -Encoding
. The line endings depend on the operating system I’d say. On Windows it is by default CRLF
while it is on Linux and Mac only LF
.
So - no - there’s nothing you can do with Export-Csv
to change the line endings. You have to do it later on with an extra step.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.