Get Last N Lines From File on Windows

Get Last N Lines From File on Windows

When working with large text files, logs, or other data-containing files on a Windows system, there often arises a need to extract the most recent or last few lines for analysis or monitoring purposes. This task is commonly encountered by system administrators, developers, and analysts. This tutorial shows how to get last N lines from file on Windows.

In PowerShell window, run the following command to create a new file for testing purpose:

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

The Get-Content cmdlet in PowerShell is used to retrieve the content of a file or files. The -Tail parameter allows specifying the number of lines from the end of the file that want to retrieve.

For example, to retrieve the last 4 lines from the file, run the following command:

Get-Content "test.txt" -Tail 4

Output example:

Line4
Line5
Line6
Line7

Leave a Comment

Cancel reply

Your email address will not be published.