In my understanding, (Get-Content $file | Measure-Object).count should return the total number of lines in the file. It does for some files, but for some generated files the result it gives is less than the correct number by 1. I examines them and see that at the last line they don't have CRLF.
Why is this the case? And how to make sure that I get the correct result? The Measure-Object documentation doesn't seem to explain this.
The behavior is unrelated to
Measure-Object:It is
Get-Contentthat reads and streams a given file's lines one by one, stripped of its trailing newline, if present on the last line.That is, to
Get-Contentit doesn't make a difference whether the last line has a trailing newline, i.e. whether the file ends with a newline or not.You need a different approach if you want to count the actual number of newlines (LF or CRLF sequences) in a file (and possibly add
1if you want to consider the final newline a line in its own right), e.g.:Alternatively:
Note the use of
-RawwithGet-Content, which reads the file as a whole into a single, (typically) multiline string, thereby preserving all newlines.