Powershell: Get-Content -ReadCount 0 does not respect -TotalCount

Created on 5 Oct 2019  路  6Comments  路  Source: PowerShell/PowerShell

Get-Content -ReadCount 0 is a convenient way to request that all lines be read _at once, into an array_ and to have that array be output _as a single_ object to the success stream.
If the intent is to read all lines into memory anyway, this can greatly speed up the command.

-TotalCount <n> allows you to limit the overall number of lines to be read from the file to a given number.

It's reasonable to expect that -ReadCount 0 also respects that number and therefore returns an <n>-element array.

Currently, -ReadCount 0 effectively _ignores_ -TotalCount and always returns _all_ lines in the file as an array.

By contrast, a specific number - e.g, -ReadCount 2 to read the file as a stream of 2-element arrays - _does_ respect -TotalCount.

Steps to reproduce

1..10 > temp:/$pid.txt

(Get-Content temp:/$pid.txt -TotalCount 3 -ReadCount 0).Count | Should -Be 3

Remove-Item temp:/$pid.txt

Expected behavior

The test should pass.

Actual behavior

The test fails:

Expected 3, but got 10.

That is, all 10 lines were read, even though only 3 were requested with -TotalCount

Environment data

PowerShell Core 7.0.0-preview.4
Area-Cmdlets-Management Hacktoberfest Issue-Bug Resolution-Fixed Up-for-Grabs

Most helpful comment

I would like to work on it.

All 6 comments

Does -TotalCount 3 -ReadCount 2 work well?

Good question - yes, it does; I've added that fact to the OP.

In the case we have a bug. Simple for hacktoberfest.

I would like to work on it.

Have at it! 馃槃

:tada:This issue was addressed in #10749, which has now been successfully released as v7.0.0-preview.5.:tada:

Handy links:

Was this page helpful?
0 / 5 - 0 ratings