Saturday, March 6, 2010

Faster get-content with ReadCount

By default, get-content reads one line at a time, presumably so you can see file content before PowerShell has finished reading the file. When you want to process large files (ie: logs) and don't want to see the raw contents, use the ReadCount parameter to speed things up. To read x lines at a time, set -ReadCount x. To read the whole file at once, set -ReadCount 0.

cat -ReadCount 0 $file

Note that this returns an array instead of a single line. The easiest way around this is to feed the resulting array down the pipeline one line at a time (still faster):

cat -ReadCount 0 $file | %{$_}

More detail here.

No comments: