r/PowerShell • • Nov 30 '25

Need help using Powershell or CMD to extract lines lots of txt files.

I'm in need of help getting Powershell (or CMD) to extract lines 7 and 13 from hundreds of txt files in a directory. I've been looking into options such as Get-ChildItem, Get-Content, Select-String, and ForEach-Object but I can't quite get them to do what I want. I've been experimenting with several configurations but the best I can get is the 7th from the first file and no further.

These files are in UTF-16 LE, which I know CMD doesn't like. So since PS plays nicer with them, I've been using it.

I'll have all the txt files in one directory and running it from there, so no need to direct it. I just need it to take the 7th and 13th lines from each file in the dir and Out-File it to Out.txt

Any help would be much appreciated, thank you.

27 Upvotes

32 comments sorted by

View all comments

0

u/[deleted] Nov 30 '25

[deleted] — view deleted comment

4

u/OlivTheFrog Nov 30 '25

I conducted a series of comparative tests between Get-Content and System.IO.Streamer.

#.txt files of varying sizes (100 iterations)
name                  Avg                  Min                  Max                 
----                  ---                  ---                  ---
Get-Content100      44.8114 Milliseconds   40.8558 Milliseconds 155.2957 Milliseconds
System.IO.Streamer100 15.3734 Milliseconds 12.7759 Milliseconds 73.8879 Milliseconds



# .txt files of average size 6100 KB (100 iterations) 
name                  Avg                  Min                  Max                 
----                  ---                  ---                  ---    
Get-Content100        53.9048 Milliseconds 47.1989 Milliseconds 265.6246 Milliseconds
System.IO.Streamer100 20.3363 Milliseconds 15.9616 Milliseconds 127.7164 Milliseconds




# .Txt files of average size 6100 KB, ut searches lines 1000 and 1100
name                  Avg                   Min                   Max                 
----                  ---                   ---                   ---    
Get-Content100        819.1446 Milliseconds 610.7799 Milliseconds 1083.3998 Milliseconds
System.IO.Streamer100 32.9905 Milliseconds  28.8792 Milliseconds  52.0951 Milliseconds

Two notable points are observed :

  • Execution time increases with file size in relatively similar proportions in both cases (Get-Content and System.IO.Streamer)
  • Execution time literally explodes when searching deep within large text files for Get-Content, whereas with System.IO.Streamer it increases only very slightly.

    • In terms of performance, System.IO.Streamer has the advantage.
    • In terms of code clarity and simplicity, Get-Content has the advantage.

I would conclude by saying that if you're searching the beginning of files, Get-Content is sufficient; otherwise, System.IO.Streamer is the clear winner due to its performance.

regards

0

u/Particular_Fish_9755 Nov 30 '25

This code seems to me to be the most efficient, even if it's a brute-force method, it will be more efficient than `Get-Content`, which loads the entire file regardless of its size, reading each file but only stopping at the line where we don't need to read further.
However, for the desired outcome, shouldn't we instead:

$results | Set-Content out.txt

The test for the currently read line also needs to be reviewed (" $lineNumber -eq "), as lines 7 and 10 do not correspond to lines 7 and 13 of the file's content. Does the file's content start from line 0 or line 1?

1

u/surfingoldelephant Nov 30 '25 edited Jun 10 '26

Get-Content, which loads the entire file regardless of its size

No, it doesn't. Get-Content streams the contents of the file line-by-line. The whole file is only read into memory if you a) specify -Raw/-ReadCount 0 or b) explicitly collect all emitted strings into memory yourself.

This completes almost instantly irrespective of file size because Get-Content streams line-by-line. Each line is emitted as a string to the pipeline one-by-one.

Get-Content foo.txt | ForEach-Object { $_ } | Select-Object -First 1

Whereas any of these will take much longer for a large file, not because Get-Content is reading the whole file into memory by default, but because I've explicitly chosen to collect each emitted string upfront.

$foo = Get-Content foo.txt

(Get-Content foo.txt) | ForEach-Object { $_ } | Select-Object -First 1

foreach ($line in Get-Content foo.txt) { $_ }

Get-Content is however quite slow, which largely comes from adding ETS properties to each string. switch -File retains line-by-line streaming, but is much quicker.