Convert CRLF Line Endings to LF with PowerShell
Strip Windows carriage returns from scripts and configs copied from Windows to WSL, Linux servers, or Git.
01Command
$ Get-Content script.sh -Raw | ForEach-Object { $_ -replace "`r`n", "`n" } | Set-Content script.sh -NoNewline02What it does
Windows text files end lines with CRLF (\r\n) while Linux and macOS use LF (\n) — a mismatch that breaks shell scripts with errors like '/bin/bash^M: bad interpreter'. Reading the file with -Raw keeps it as one string, then -replace swaps the two-character sequence for a bare newline. The reverse command restores Windows line endings if a tool needs them.
03Risk
04Undo
$ $c = Get-Content script.sh -Raw; $c -replace "`n", "`r`n" | Set-Content script.sh -NoNewline05Sources
- Set-Content (Microsoft.PowerShell.Management)Evidence: Documents the -NoNewline parameter so no extra trailing newline is appended when writing the result.
- about_Special_CharactersEvidence: Defines the backtick escape sequences `r (carriage return) and `n (new line) used in the replacement string.
Last verified: 2026-09-19 · Category: Files & Folders
06Related solutions
Diagnose NTFS file system errors and bad sectors on any volume — including the system drive while Windows is running — before committing to a repair.
Robocopy is Windows' built-in robust file copier: use /MIR to keep a destination folder as an exact one-way mirror of a source, resumable and restartable.
Two-step command sequence that makes you the owner of a locked file tree and grants yourself full control — the standard fix for 'Access is denied' on old profiles and OS reinstall leftovers.