Search code examples
windowsunixpowershellnewline

UNIX format files with Powershell


How do you create a unix file format in Powershell? I am using the following to create a file, but it always creates it in the windows format.

"hello world" | out-file -filepath test.txt -append

As I understand, the new line characters CRLF make it to be a Windows format file whereas the unix format needs only a LF at the end of the line. I tried replacing the CRLF with the following, but it didn't work

"hello world" | %{ $_.Replace("`r`n","`n") } | out-file -filepath test.txt -append

Solution

  • One ugly-looking answer is (taking input from dos.txt outputting to unix.txt):

    [string]::Join( "`n", (gc dos.txt)) | sc unix.txt
    

    but I would really like to be able to make Set-Content do this by itself and this solution does not stream and therefore does not work well on large files...

    And this solution will end the file with a DOS line ending as well... so it is not 100%