How to Remove the First and Last Lines from a File Using PowerShell?

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.txt

Method 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:

  1. Use Get-Content to read the contents of the file into memory.
  2. Use the -Skip parameter to skip the first line and the Select-Object cmdlet with the -SkipLast parameter to remove the last line.
  3. Use Set-Content to 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
$modifiedContent

Note 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:

Remove the First and Last Lines from a File Using PowerShell

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:

  1. Create a temporary file using the New-TemporaryFile cmdlet.
  2. Use Get-Content to read the original file, skipping the first line and piping the output to Set-Content to write it to the temporary file.
  3. Use Get-Content to read the temporary file, skipping the last line and piping the output to Set-Content to write it back to the original file.
  4. Remove the temporary file using the Remove-Item cmdlet.

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 $tempFile

This 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.txt

This 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.