UNIX format files with Powershell
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%
I've found that solution:
sc unix.txt ([byte[]][char[]] "$contenttext") -Encoding Byte
posted above, fails on encoding convertions in some cases.
So, here is yet another solution (a bit more verbose, but it works directly with bytes):
function ConvertTo-LinuxLineEndings($path) { $oldBytes = [io.file]::ReadAllBytes($path) if (!$oldBytes.Length) { return; } [byte[]]$newBytes = @() [byte[]]::Resize([ref]$newBytes, $oldBytes.Length) $newLength = 0 for ($i = 0; $i -lt $oldBytes.Length - 1; $i++) { if (($oldBytes[$i] -eq [byte][char]"`r") -and ($oldBytes[$i + 1] -eq [byte][char]"`n")) { continue; } $newBytes[$newLength++] = $oldBytes[$i] } $newBytes[$newLength++] = $oldBytes[$oldBytes.Length - 1] [byte[]]::Resize([ref]$newBytes, $newLength) [io.file]::WriteAllBytes($path, $newBytes)}