r/PowerShell • u/Man-e-questions • Feb 23 '26
How to iterate through 50 lines of a large CSV each day?
Lets say i have a CSV file containing 1,0000 rows, that i want to loop some powershell commands through 50 rows at a time, each day, without repeating any rows as the powershell commands will effectively delete the data from that row, until i go through all rows. What would be the best way to do this?
62
Upvotes
15
u/420nukeIt Feb 24 '26 edited Feb 24 '26
I’d add an extra value to the csv rows like processed, set this to false to every entry, then as you run the script it changes the value to true. If you want to keep track you can add another value for date_processed, ticket_number etc.
You just select the rows that have processed -eq false for whatever count you set
2
u/TheBigBeardedGeek Feb 23 '26
I'd run the CSV file through a loop to split it into 50 lines each and export them out. Then you can just run the next file the next day
Probably add in extra logic to track which has been done or something
111
u/Dry_Duck3011 Feb 23 '26
Select -first 50
Select -skip 50 -first 50
82
u/rwh4vt Feb 23 '26
I'm getting a "I'm trying to send 50 spam emails everyday from this list of emails I bought" vibes.
Can you clarify what the purpose is or why only 50?
It seems if it's a legitimate workflow the solution you are trying to implement cannot be the best way to do it.
44
u/metekillot Feb 23 '26
Just rewrite the CSV each day without the 50 rows you read already.
20
u/jeffrey_f Feb 23 '26
I must ask, why just 50/day?
8
u/IllTutor8015 Feb 23 '26
Why do you exactly have those constraints with the 50 rows each day only? Seems like a very specific yet bizzare requirement? What's the justification for that exactly? As in, why can't you just go through the whole file until it finishes? As the fastest that comes to mind would to start sleep for 23h or such and let it continue work the following day. Since you didn't mention turning off the ps session? Can you expand on the whole idea or provide more details?
4
u/Carribean-Diver Feb 23 '26
Script imports and processes the csv and renames it for archive purposes. Originating process appends data to the csv daily.
4
u/annalesinvictus Feb 23 '26
I would use a progress json file to track the index of what row you left off at the previous day. Pick up from there the next day until no rows left.
4
u/purplemonkeymad Feb 24 '26
Sqlite.
Put all that in a sqlite db, then use an atomic select and update query to get 50 at a time. At the end of each item you then run a command to update that row to "done." (Typically you'll want a last pulled time, and status ["tobedone" or "done"] column, that way you can identify stuck jobs.)
You could just rewrite the file, but errors might cause you to skip items that need to be re-done.
3
u/AstroCynical Feb 23 '26
You could always rewrite the CSV without the extra lines you’ve processed each day.
Otherwise, if the code you need to run each day is the same, you could write it out inside a function using a for loop, where you pass in the value of $i and the upper bound as parameters.
Hope that helps
15
u/be_easy_1602 Feb 23 '26
We hate “AI”, but this is a really simple task for it, that actually is a legit use case for an LLM if you want it done fast.
1
u/TheW0ndaKid Feb 23 '26
Single execution or rerunning the same script each day?
2
u/NorCalFrances Feb 23 '26
Any reason to not just loop 50 times, reading a row each time & either deleting the line after it's processed or writing it out? You can keep track of what row you left off at in so many ways...
2
u/obsidianih Feb 23 '26
Two scripts - one splits into files with 50 lines in each one. Second just take 1 file and once done delete the file.
2
u/SearingPhoenix Feb 24 '26 edited Feb 24 '26
Needs pwsh for the Get-Random -Count
Importing the CSV into a list of objects allows the .Remove() method, compared to a standard array which will complain about fixed size.
2
u/BOT_Solutions Feb 24 '26
I’d avoid trying to track “which 50 did I process today” inside the script logic itself. That usually gets messy fast.
Instead, I’d treat it more like a queue. Either move processed rows out into a separate file as you go, or maintain a simple state marker somewhere so you always know where you left off.
Another clean approach is to split the CSV into smaller batches up front and just process one file per day. That way you’re not constantly re-reading and filtering the full 10,000 rows.
The key thing is making it idempotent. If the script runs twice or fails halfway through, you don’t want it reprocessing or skipping data unintentionally. So whatever approach you take, make sure “processed” is clearly defined and persisted somewhere outside of memory.
Personally, I lean toward updating the source after each batch or writing remaining rows back out. That keeps it simple and transparent.
2
u/Flabbergasted98 Feb 24 '26
For the love of godot and all that is binary.
Switch to SQL.
2
u/cbass377 Feb 24 '26
50 lines a day, 1000 lines, 200 days.
Skip 50 x (daynumber - 1)
$X = 50*(daynumber -1)
Select -skip $X
Process 50 lines.
day 1, you skip 0 lines,
day 2, you skip 50 lines
day 3, you skip 100 lines
and so on.
2
u/Ok_Mathematician6075 Feb 25 '26
I must ask. what the heck you are doing.
4
u/nkasco Feb 23 '26
1000 rows is nothing, but at scale you'll want to use something like CSVReader or converting your CSV to a DataTable or else filtering will be extremely painful.
1
u/fasteasyfree Feb 23 '26
Unless you're massively memory constrained, just: Load the entire csv into memory. In a foreach loop ($Row in $CsvRows), use a counter. If statement, 50 or less, perform your action. Over fifty, direct assignment to a new variable. Export that new variable to a new csv.
The script could be made clever by using Get-Childitem to grab all the CSVs and sort by the created date to load the newest one.
If memory consumption is a concern, you could modify the above to use System.IO.StreamReader ReadLine method, and StreamWriter, to avoid gobbling up the entire CSV at once.
1
u/nautitrader Feb 24 '26
How many is 1,0000 rows?
1
u/ankokudaishogun Feb 24 '26
To start, this seems a X-Y problem. What exactly are you trying to accomplish?
That said, I'd guess the easiest ways would be:
.DayOfYearproperty of[Datetime]object and feeding the script the first day you want it to run1
u/Kaligraphic Feb 24 '26
Does it have to be exactly 50? Can you just wait half an hour between lines?
1
u/lunagra80 Feb 25 '26
If you don't need anything fancy, the logic can be easily this
x = import-csv --Path <file> rows_to_process = x | select-object -First 50
this basically gets the rest of the rows skipping the first 50 and overwrites the original file with just them
rows_to_skip = x | select-object -Skip 50 | Export-Csv --Path <file> <Insert here whatever you want to do with the 50 records>
Done!
PowerShell is great at turning everything into an object you can easily navigate. Most commands like import-csv or Invoke-restmethod just return the same type of file and you can use it easily within a loop with not much effort.
Once you have an object there are a lot of cmdlet / functions you can use to "select" a range, "where" a value is something, or just get "unique" values, etc that you can just use with the |, and assign to a variable directly the subset of items you need from the object leaving everything else behind
Most times you don't need fancy code when working with CSVs
You don't need to "delete the records you process" from the csv because you can just get the 50 you need, save the rest into the same file overwriting it. Actually every day your script becomes a little faster because you have less and less records :) You don't need to split the records in separate files with 50 records each You don't have to manage file names with dates and stuff like that.
The file has the same name.
If you want to know which records have been processed which day. You can always add some logging commands to a log file while looping into those records
1
u/BinarySpike Feb 26 '26
Get the day number, multiply it by 50, that's your starting record index. Loop through and grab 50 records.
It leaves your CSV file untouched.
1
u/The82Ghost Feb 24 '26
show us the code you have and we may be able to help you.
1
u/mycocomelon Feb 24 '26
Can you use a Python library like duckdb or polars and call it from powershell?
0
u/redtollman Feb 24 '26
how many rows in the csv? these new numbers mess me up. From power shell, open WSL, then
head -n 50 myfile.csv > lines_to_process.csv
sed -i '1,50d' myfile.csv
0
u/MNmetalhead Feb 24 '26
What have you done so far and what was the result?
-11
u/Pin_Physical Feb 23 '26
Hate to say it, but I'd ask Gemini or Claude
-6
u/thainfamouzjay Feb 24 '26
Throw it into AI
-20
u/brazzala Feb 23 '26
Use the copilot bro and learn what it does...
-6
u/Tilt23Degrees Feb 24 '26
This is a 10 minute fix with AI. ….