r/PowerShell • u/Zandizar • 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.
View all comments
Show parent comment
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.txtThe 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?