Get First N Lines From File on Windows

Get First N Lines From File on Windows

When working with files on a Windows system, the ability to quickly access and extract specific portions of their content is a common requirement. Whether it's for previewing the initial lines of a log file, reviewing the header of a large dataset, or just obtaining a quick overview of a text-based document, being able to efficiently retrieve the first N lines is a valuable skill. This tutorial demonstrates how to get the first N lines from a file on Windows.

In PowerShell window, execute the following command to create a new file for testing purposes:

echo "Line1`nLine2`nLine3`nLine4`nLine5`nLine6`nLine7" > test.txt

Using the Get-Content cmdlet with the -Head parameter, we can effortlessly retrieve the first N lines from a file.

For instance, to retrieve the first 4 lines from the file, run the following command:

Get-Content "test.txt" -Head 4

Output example:

Line1
Line2
Line3
Line4

Leave a Comment

Cancel reply

Your email address will not be published.