r/PowerShell • u/misanthrope5 • Jan 01 '26
Batch removing first N lines from a folder of .txt files
Hi, I'm new to Powershell, and hoping this isn't too dumb a Q for this sub. I've got a folder of 300+.txt files named:
- name_alpha.txt
- name_bravo.txt
- name_charlie.txt
etc etc etc
Due to the way I scraped/saved them, the relevant data in each of them starts on line 701 so I want a quick batch process that will simply delete the first 700 lines from every txt file in this folder (which consists of garbage produced by an HTML-to-text tool, for the most part).
I've used .bat batch files for text manipulation in the past, but googling suggested that Powershell was the best tool for this, which is why I'm here. I came across this command:
get-content inputfile.txt | select -skip 700 | set-content outputfile.txt
Which did exactly what I wanted (provided I named a sample file "inputfile.txt" of course). How can I tell Powershell to essentially:
- Do that to every file in a given folder (without specifying each file by name), and then
- Resave all of the txt files (now with their first 700 lines removed)
Or if there's a better way to do all this, open to any help on that front too! Thank you!
15
u/BlackV Jan 01 '26 edited Jan 01 '26
yes that is exactly what you want
then you need to add that to a loop, the best way (especially when testing/learning) is a
foreach ($SingleItem in $AllFiles)loopget-childitemor manually with an variable/array)foreach$SingleItem) get the content, skip the700and set the contentit's exactly what youve done already but in a loop
notes:
get-childitemis getting all the txt file in a path you specify$SingleItemis a real file object that you are working with, that item can be used withget-content/set-content/rename-item/whateverget-childitemcommand adn then directly runforeach ($SingleItem in $AllFiles) {}this will do nothing but means you can validate AND test with$SingleItembefore running through all files6
u/Mutsy007 Jan 01 '26
Don't forget, backup is your friend! You are manipulating live data, either work on a copy of the data or write the new data to a different folder. If you run the script a second time in error, powershell will quickly remove another 700 lines of text in all files.
6
u/da_chicken Jan 01 '26
First, I'd make an output directory where you want them to go.
Then you just enumerate the files, and then loop through them:
By default, this will change the encoding to Unicode (UTF-16-LE). You may want to specify
Set-Content -Path $NewFileName -Encoding ansior-Encoding asciior-Encoding utf8nobomdepending on what you're doing. But beware that unless you're on Powershell v7+ that UTF-8 defaults to including a byte order mark andutf8nobomdoesn't exist. Linux systems will puke on BOMs in UTF-8 pretty frequently even though the standard permits them. If you know you only have simple characters, I strongly recommend just usingascii.-3
u/OlivTheFrog Jan 01 '26
Since an AI was able to provide you with an answer, modify your query. Low effort on your part doesn't warrant much more. Help the others, yes. Do the job for them, no.
Here's a hint : Get-childItem and Foreach
-4
u/MasterpieceGreen8890 Jan 01 '26
Chatgpt.. you can also just add variables and loop in your same bat file since it's already working