Copying the First n Lines of a Text File using PowerShell

I sometimes work with very large text files. A common task for me is to make a copy of the first few lines of the large text file I’m working with so I can work with the smaller file until I’m ready to deal with the huge file. To grab the first few lines of a text file in Unix you can use the head command. You’d think that the robocopy.exe command line utility would have a parameter to specify the number of lines of a text file to copy but as far as I can tell robocopy.exe doesn’t have such a parameter. Anyway, the easiest way I’ve found to copy the first n lines of a text file is to use the get-content PowerShell cmdlet like so:

PS > get-content .\hugeFile.txt -totalcount 25 | set-content sampleFile.txt

This command grabs the first 25 lines of hugeFile.txt and saves as sampleFile.txt. You can also use the > operator instead of set-content like so:

PS > get-content .\hugeFile.txt -totalcount 25 > sampleFile.txt

Not such a good idea is this approach:

PS > (get-content .\hugeFile.txt)[0..24] > sampleFile.txt

This approach grabs the entire contents of hugeFile.txt first, and then extracts the first 25 lines. If hugeFile.txt is huge then the command could take a very long time.

Update: This technique may have problems. I used this PowerShell technique to copy a few lines from a very large text file to a small test file. When I tried to import the small test file into a SQL database using the bcp.exe utility, I got errors. It looks like maybe this PowerShell technique inserts some sort of control characters, probably at the end of lines or perhaps near the tab delimiters. I’m not sure exactly what went wrong but until I know, when the result of a copy must be used in some other system, I’m falling back to writing a short C# program to make the copy of the first n lines of a text file.

This entry was posted in Software Test Automation. Bookmark the permalink.