Jeff Wouters's Blog

Remove white lines in file through PowerShell

by on Nov.25, 2013, under DuPSUG, PowerShell

This morning I found myself in the need of removing all white lines in a file of more than 12.000 lines.
Since it’s a white line, you can’t use find&replace in the GUI of notepad… you’ll need something to replace and a white line isn’t something; it’s nothing.
So here’s the PowerShell solution 🙂

(Get-Content d:\temp\MyFile.ps1) | ? {$_.trim() -ne ""} | Set-Content d:\temp\MyFile.ps1

Leave a Reply