Remove N first or last lines from text file in PowerShell

This tutorial will show you how to remove N first or last lines from text file in PowerShell. I will use PowerShell 7, in case of you are wondering what is my platform version. From time to time I have a task to remove N first or last lines from text file with variations. PowerShell is a helpful built in tool for such a task. It includes the range operator which can be of help here.

First of all, we heed a file for testing. Perhaps the fastest way to create a text file with a given number of lines is to use the Set-Content cmdlet of Power Shell in conjunction with the range statement . In the third line, I put the contents of the file into a variable, which I will further process in the script.

Open PowerShell and execute the following lines, one by one.

$file = "C:\barbara\file.txt"

Set-Content $file (1..100) -Force

$content = Get-Content $file

PowerShell Create A Text File with Lines

It is important to note that the Set-Content cmdlet adds an empty line at the end of the file. In this very task it doesn’t affect anything, but keep this in mind. You will see why it is important.

Now we have our file, let’s try to see how to remove the lines.

Remove N first lines from text file with PowerShell

$content.length is the length of the array of file lines, created by the Get-Content cmdlet, i.e. index of the last element (excluding the last empty row added by Set-Content!) To remove first 10 lines, we need to take lines from the 11th (its index is 10) to the end.

$content[10..($content.length-1)]|Out-File $file -Force

PowerShell Remove N First Lines From Text File

That’s pretty simple. Now let’s delete last lines from the same file.

Remove N last lines from text file with PowerShell

The logic is the same here. Index 0 is the first row, $content.length-11 excludes the last 10 rows from the selection. We just save everything that is in between.

$content[0..($content.length-11)]|Out-File $file -Force

PowerShell Delete N Last Lines From Text File

Delete everything except the last 10 lines

Finally, the following command will help you to remove everything above the last 10 lines in your text file.

$content[($content.length-11)..($content.length-1)]|Out-File $file -Force

PowerShell Delete All Except N Last Lines From Text File

Using the $content variable you can easily get a specify line from the file. As it is an array, any array element can be referenced by its ordinal number, just keep in mind that it is a zero-based indexing system. For example, type  $content[2] in PowerShell to see the value of the third element of an array. This is quite obvious.

PowerShell Print A Line

To get the last element of the value without knowing the length of the array, use the command like this: $content[$content.length-1]. But there is also a shorter version: $content[-1]. For penultimate element use $content[-2] and so on.

Looks like you've read the post to the end. If you enjoyed it, please share it. You would greatly help our blog to grow!

Author: The MFTNEXT Team

The MSFTNEXT project is a small team of authors who love to engage with the latest technology and gadgets. Being passionate Windows bloggers, we are happy to help others fix their system issues.

Leave a Reply

Your email address will not be published.