Unify line endings

Let’s assume you have a software project and the line endings of the files are inconsistent. Meaning, that git commits and pushes without proper configuration will result in a merge disaster because the merge tools recognize the whole file as changed and not just the lines actually changed. When working on Windows or on a system that supports PowerShell you can use the following piece of code to change the line endings of all files in your project folder:

Get-ChildItem -File -Recurse | ForEach-Object {
    try {
        $Content = Get-Content -Raw -Path $_.FullName
        $Content = $Content.Replace("`r`n", "`n")
        Set-Content -Path $_.FullName -Value $Content -NoNewline
    }
    catch {
        Write-Host -Object "Error handling file $($_.FullName)"
    }
}

This will load the content of each file, replace “\r\n” (Windows line endings) with “\n (Linux line endings) and write the content of the file into the same file it was read from.