Olaf has provided the crucial pointer in a comment; let me flesh it out:
Get-Content
's -ReadCount
parameter sends arrays (batches) of lines read from the input file through the pipeline.
Therefore, the automatic $_
variable in the receiving ForEach-Object
call then refers to an array of lines rather than a single line, as would be the case without -ReadCount
.
With an array (collection) as the LHS, PowerShell's comparison operators such as -match
act as filters and return the sub-array of matching items rather than a Boolean.
- In the case of
-match
, specifically, using an array-valued LHS additionally means that the automatic $Matches
variable, which reports what text the -match
operation captured, is not populated
To put it differently:
While using Get-Content -ReadCount
can speed up processing of text file in combination with ForEach-Object
, you need to iterate over the elements of the array reported in $_
to get line-by-line processing:
Get-Content c:\temp\myfile.txt -ReadCount 10 |
ForEach-Object { foreach ($line in $_) { $line -match 'my_string' } }
Note that it is the common -OutBuffer
parameter that would have behave as you expected: -OutBuffer $n
means that the cmdlet at hand collects 1 + $n
objects before outputting to the pipeline, at which point, however, they are output one by one, as usual.
That said, unless the value of $n
is large, the rarely used -OutBuffer
parameter provides no performance benefit, and may even slow things down a bit.