PowerShell: Convert Linux/Windows Line Ending
Here's functions to report or convert newline convention.
put this in your PowerShell Profile
function xah-newline-report { # .DESCRIPTION # xah-newline-report accept a piped file object. It print one of # - Windows CRLF ‹filename› # - MacOS9 CR ‹filename› # - unix LF ‹filename› # - No CR nor LF ‹filename› # Version 2021-12-16 2022-01-15 # .EXAMPLE # dir -Recurse -file -Include "*html" | xah-newline-report # .LINK # http://xahlee.info/powershell/powershell_fix_newline.html Process { $content = [IO.File]::ReadAllText($_) if ($content.contains("`r`n")) { "Windows CRLF $_" } elseif ($content.contains("`r")) { "MacOS9 CR $_" } elseif ($content.contains("`n")) { "unix LF $_" } else { "No CR nor LF $_" } } }
function xah-newline-to-unix { # .DESCRIPTION # xah-newline-to-unix accept a piped file object, it convert line ending to unix convention. # .EXAMPLE # dir -Recurse -file -Include "*html" | xah-newline-to-unix # version 2021-12-16 2022-01-15 # .LINK # http://xahlee.info/powershell/powershell_fix_newline.html Process { $content = [IO.File]::ReadAllText($_) if ($content.contains("`r`n")) { Copy-Item $_ ($_.fullname + (Get-Date -Format "~yyyyMMddHHmm~")) $newContent = ($content -replace "`r`n", "`n") [IO.File]::WriteAllText($_, $newContent) } elseif ($content.contains("`r")) { Copy-Item $_ ($_.fullname + (Get-Date -Format "~yyyyMMddHHmm~")) $newContent = ($content -replace "`r", "`n") [IO.File]::WriteAllText($_, $newContent) } } }