How to get the last line of a file using Powershell?

Powershell can be used to read the last line of a file. In other words, we can use Powershell cmdlets to get the last lines of a file.

There are multiple ways by which you can get the last line of a file in Powershell. Some of these methods are efficient and others may consume more time than you may like.

Some of the ways by which you can read the last line of a file include the GetContent cmdlet with tail and select options. Out of these two options, the tail option is faster and fetches the result without loading the entire file in memory.

The select option also fetches the last line of a file. However, due to the way it handles memory tasks, using the select option will take more time. We will look at both ways below with examples.

Get last line using tail

The command to fetch the last line of a text file is:

Get-Content -tail 1 $path

$Path is a variable that stores the file location.

For example, if we were to fetch the last line of a file named sample.txt from under d:\test_folder_1, the command will become:

Get-Content -tail 1 d:\test_folder_1\sample.txt

The result of this command is represented in the screenshot below:

Last line of a file using tail option.

The good thing with this command is that the file read operation is faster on account of the tail option. So, it is able to generate the output much faster than loading the entire file in the memory and then loading the last line of the file.

Get last line using Select last

An alternate command to fetch the last line of a file uses the ‘Select’ parameter as per the command shared below:

Get-Content $Path | select -last 1

$Path is the variable that holds the location of the file that needs to be searched on. In our example, we will use the static file location to fetch the last line from the file.

Get-Content d:\test_folder_1\sample.txt | select -last 1

The command above will fetch the last line from the sample.txt file from the folder location mentioned here.

The output of this command is represented below in the screenshot.

Read last line of file using select.

You can see that both commands produce the same output and fetch the last line of the text file. The command will -tail option is faster though.

Once you have fetched the last line of a file, you could run the output through logical operators to check for specific expressions or characters. For example, you may run through the file’s last line to check if the last line contains blank space or new line character, or any other specific character. Based on the results, you could then run further processing on the file or your program.

Suggested Powershell Tutorials

You may like to read related Powershell tutorials to enhance your knowledge about Powershell on Windows computers:

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Rajesh Dhawan

Rajesh Dhawan is a technology professional who loves to blog about smart wearables, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.