Ignoring Empty Lines

by | January 12,2012

Table of Contents

Reading Text Files and Skipping Blank Lines

To read in a text file and skip blank lines, try this:

$file = 'c:\sometextfile.txt'
Get-Content $file | Where-Object { $_.Trim() -ne '' }

It will omit empty lines, lines with only blanks and lines with only tabs.

ReTweet this Tip!