As an IT professional, I often need to manipulate text files in various ways. One common task is removing the first and last lines from a file, which can be useful for cleaning up headers, footers, or other unwanted content. In this tutorial, I’ll show you several methods to remove the first and last lines from a file using PowerShell.
For all the methods, I will use the sample.txt file below. If you want to create the file, you can use the PowerShell code below.
@"
United States of America
Capital: Washington, D.C.
Largest State: Alaska
National Bird: Bald Eagle
Currency: US Dollar (USD)
"@" | Set-Content C:\Bijay\sample.txtMethod 1: Using the Get-Content and Set-Content Cmdlets
The best way to remove the first and last lines from a file is by using the Get-Content and Set-Content cmdlets in PowerShell. Here’s how it works:
- Use
Get-Contentto read the contents of the file into memory. - Use the
-Skipparameter to skip the first line and theSelect-Objectcmdlet with the-SkipLastparameter to remove the last line. - Use
Set-Contentto write the modified content back to the file.
Here’s an example command that removes the first and last lines from a file named “sample.txt”:
$modifiedContent = Get-Content C:\Bijay\sample.txt | Select-Object -Skip 1 | Select-Object -SkipLast 1
$modifiedContentNote that this method requires PowerShell version 3.0 or later, as the -SkipLast parameter was introduced in that version.
I executed the above PowerShell script using VS code and you can see the exact output in the screenshot below:

Check out Create CSV Files with Headers in PowerShell
Method 2: Using the ReadLines() Method
Another approach is to use the ReadLines() method in PowerShell to read the file into an array, remove the first and last elements, and then write the array back to the file. Here’s an example:
$content = [System.IO.File]::ReadLines("sample.txt")
$content = $content[1..($content.Length-2)]
[System.IO.File]::WriteAllLines("sample.txt", $content)In this method, we use the ReadLines() method to read the file into an array, then use array indexing to remove the first and last elements. Finally, we use the WriteAllLines() method to write the modified array back to the file.
Read Find All Files with a Specific Extension Using PowerShell
Method 3: Using a Temporary File
If you’re working with large files and want to avoid reading the entire file into memory, you can use a temporary file to store the modified content. Here’s an example:
- Create a temporary file using the
New-TemporaryFilecmdlet. - Use
Get-Contentto read the original file, skipping the first line and piping the output toSet-Contentto write it to the temporary file. - Use
Get-Contentto read the temporary file, skipping the last line and piping the output toSet-Contentto write it back to the original file. - Remove the temporary file using the
Remove-Itemcmdlet.
Here’s the PowerShell code:
$tempFile = New-TemporaryFile
Get-Content sample.txt | Select-Object -Skip 1 | Set-Content $tempFile
Get-Content $tempFile | Select-Object -SkipLast 1 | Set-Content sample.txt
Remove-Item $tempFileThis method is particularly useful when dealing with large files, as it avoids loading the entire file into memory.
Check out How to Use PowerShell to Find Files Modified After a Certain Date?
Remove Lines Based on Content
In some cases, you may want to remove lines from a file based on their content rather than their position. For example, let’s say you have a log file and want to remove all lines containing the word “error.” You can achieve this using the Select-String cmdlet:
Get-Content sample.txt | Select-String -NotMatch "error" | Set-Content sample.txtThis command reads the file, filters out any lines containing the word “error,” and then writes the remaining lines back to the file.
Handle Large Files
When working with very large files, such as those in the gigabyte range, you may encounter performance issues or even run out of memory when using the methods described above. In these situations, you can use a combination of the StreamReader and StreamWriter classes to process the file line by line:
$reader = New-Object System.IO.StreamReader("sample.txt")
$writer = New-Object System.IO.StreamWriter("temp.txt")
$reader.ReadLine() | Out-Null # Skip the first line
while (($line = $reader.ReadLine()) -ne $null) {
$writer.WriteLine($line)
}
$reader.Close()
$writer.Close()
$reader = New-Object System.IO.StreamReader("temp.txt")
$writer = New-Object System.IO.StreamWriter("sample.txt")
while (($line = $reader.ReadLine()) -ne $null) {
if ($reader.BaseStream.Position -lt $reader.BaseStream.Length) {
$writer.WriteLine($line)
}
}
$reader.Close()
$writer.Close()
Remove-Item "temp.txt"This code stores the modified content in a temporary file, reading and writing the files line by line to minimize memory usage.
Conclusion
In this tutorial, I’ve shown you several methods for removing the first and last lines from a file using PowerShell. Whether you’re working with small or large files, there’s a solution that will fit your needs. Remember, when working with essential files, always create a backup before modifying to avoid data loss.
You may also like:
- Create a Log File with Date and Time in PowerShell
- How to Log Error Messages to a File Using PowerShell Try Catch?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.