Extract only the first 10 lines of a csv file in powershell

PowershellCsv

Powershell Problem Overview


I have csv file and want to output a new csv file, that only contains the first 10 lines form the original one. I've so far only found code to delete single lines or lines that contain a certain word. Its probably a 15 character one-liner, but I am not sure how to approach this, any help would be greatly appreciated. I don't expect you to write code, just the right command would help me out.

Powershell Solutions


Solution 1 - Powershell

Get-Content "C:\start.csv" | select -First 10 | Out-File "C:\stop.csv"

That did it

Solution 2 - Powershell

Get-Content in.csv -Head 10 > out.csv

Solution 3 - Powershell

Using -TotalCount parameter is recommended. Especially, if you have a big CSV file. By this way, you will only read the first 10 lines of your CSV file instead of reading all lines, selecting the first 10 lines and extracting them.

Get-Content C:\Temp\Test.csv -TotalCount 3 | Out-File C:\Temp\Test10lines.csv

This works in Powershell 2 onwards.

Solution 4 - Powershell

You can also use

(Get-Content yourfile.csv)[0 .. 11]>outputfile.csv

Be sure to include the header row or you could run into issues. You can also change the numbers to get whatever rows you want. The first and head flags did not work for me.

Solution 5 - Powershell

Windows PowerShell

Get-Content -First 10'C:\Users\jason\Desktop\big_file.csv'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMonacco FranzeView Question on Stackoverflow
Solution 1 - PowershellMonacco FranzeView Answer on Stackoverflow
Solution 2 - Powershelluser2226112View Answer on Stackoverflow
Solution 3 - PowershellemekmView Answer on Stackoverflow
Solution 4 - PowershellJosh GroenView Answer on Stackoverflow
Solution 5 - PowershellNilesh ButaniView Answer on Stackoverflow