PowerShell function to remove empty lines

For some time no I have to remove empty (white) lines from a text file or an array.
Here’s the function I wrote that I would like to share with you… I hope you’ll find it useful 🙂

You can use it like so:

$array = ‘a’,’b’,”,’c’,’d’,”,”,’e’
$Array | Remove-EmptyLines

Remove-EmptyLines -InputObject $array

Remove-EmptyLines -InputObject (Get-Content myfile.txt)

Get-Content myfile.txt | Remove-EmptyLines

One comment

  1. Harro says:

    Much appreciated for sharing!

    Works like a charm;
    in contrary to the often seen and not functioning
    (gc file.txt) | ? {$_.trim() -ne “” } | set-content file.txt

    Cheers
    Harro

Leave a Reply

Your email address will not be published. Required fields are marked *