question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get-Content -ReadCount 0 does not respect -TotalCount

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
eugenesmlvcommented, Oct 9, 2019

I would like to work on it.

2reactions
iSazonovcommented, Oct 7, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get-Content -ReadCount 0 combined with -Last / -Tail ...
Get-Content -ReadCount 0 is a convenient way to request that all lines be read at once, into an array and to have that...
Read more >
Examples of PowerShell Get-Content
Read Count : Specifies the number of lines of the content sent through the pipeline at a time. The Default value is 1...
Read more >
Get-Content (Microsoft.PowerShell.Management)
Specifies how many lines of content are sent through the pipeline at a time. The default value is 1. A value of 0...
Read more >
PowerShell Get-Content -ReadCount
1 Answer 1 · Get-Content 's -ReadCount parameter sends arrays (batches) of lines read from the input file through the pipeline. · Therefore,...
Read more >
PowerShell Open File | Syntax & Parameters of ...
Truncate: Specifies that the operating system should open an existing file. When the file is truncated so that its size should be zero...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found