共用方式為


adding line counts to your items in the pipeline

One of the things I've noticed I find helpful is the "pipeline snippets" I use over and over - most often it's something that adds a member or method to the objects.  While it's not the most efficient possible approach by any means, a quick-and-dirty way of getting line counts is leveraging get-content's default behavior of splitting on lines and then using the resulting count.

| %{ add-member -in $_ NoteProperty LineCount $(gc $_.FullName | measure-object).Count -passthru }

The -passthru is the important point, as we're consuming the input object from the pipeline and we need to generate an output object back for the later stages of the pipeline.

With this little snippet (which, yes, we could make into a function/filter, I just haven't gotten around to that yet), we can then easily add a LineCount property which later stages can treat as a first-class citizen, for sorting/displaying/whatever.

So, for instance, you could display your SQL files based on line count data with:

dir -r -filter *.sql
| %{ add-member -in $_ NoteProperty LineCount $(gc $_.FullName | measure-object).Count -passthru }
| sort -desc LineCount
| ft -a FullName,LineCount