How to Remove the Last Empty Line from a File Using PowerShell?

Recently, I got a requirement to remove the last empty line from a file using PowerShell. There are various methods to do it. In this tutorial, I will show you different methods to remove the last empty line from a file using PowerShell with examples.

Method 1: Using Get-Content, Where-Object, and Set-Content

The best way to remove the last empty line from a file in PowerShell is by using a combination of Get-Content, Where-Object, and Set-Content cmdlets. Here’s how you can do it:

(Get-Content -Path "C:\Bijay\SampleFile.txt") | Where-Object { $_.Trim() -ne "" } | Set-Content -Path "C:\Bijay\UpdatedSampleFile.txt"

Let’s break this down:

  1. Get-Content reads the content of the file and passes it down the pipeline.
  2. Where-Object filters out any lines that are empty or contain only whitespace characters using the Trim() method and the -ne (not equal to) operator.
  3. Set-Content writes the filtered content back to the same file, effectively removing the last empty line.

You can see the exact output in the screenshot below. It created a different file that does not contain blank lines.

Remove the Last Empty Line from a File Using PowerShell

Check out Create a File with Date in the Name Using PowerShell

Method 2: Using a Regular Expression

Another way to remove the last empty line is by using a regular expression with the Split() method and the Net class. This method is particularly useful when you want to remove multiple variations of line breaks. Here’s how you can implement it:

$content = Get-Content -Path "C:\Bijay\SampleFile.txt" -Raw
$content = $content -replace "(\r?\n|\r)+$"
$content | Set-Content -Path "C:\Bijay\UpdatedSampleFile.txt"

In this method:

  1. Get-Content reads the content of the file as a single string using the -Raw parameter.
  2. The regular expression (\r?\n|\r)+$ matches one or more occurrences of either \r\n (Windows-style line break), \n (Unix-style line break), or \r (Mac-style line break) at the end of the string.
  3. The -replace operator replaces the matched line breaks with an empty string, effectively removing the last empty line.
  4. Set-Content writes the modified content back to the file.

This method is handy when dealing with files that may have different types of line breaks.

Check out Create XML Files with Content Using PowerShell

Method 3: Using the Trim() Method

If you prefer a more concise approach, you can use the Trim() method to remove the last empty line along with any trailing whitespace. Here’s an example:

(Get-Content -Path "C:\Bijay\SampleFile.txt" -Raw).Trim() | Set-Content -Path "C:\Bijay\UpdatedSampleFile.txt"

In this one-liner:

  1. Get-Content reads the content of the file as a single string using the -Raw parameter.
  2. The Trim() method removes any whitespace characters, including line breaks, from the beginning and end of the string.
  3. Set-Content writes the trimmed content back to the file.

This method is perfect when you want to remove not only the last empty line but also any trailing whitespace characters.

Check out Remove Blank Lines from CSV Files Using PowerShell

Removing Empty Lines from the End of a File in PowerShell

Sometimes, you may have multiple empty lines at the end of a file, and you want to remove all of them. You can achieve this by using a slightly modified version of the first method:

(Get-Content -Path "C:\Bijay\SampleFile.txt") | Where-Object { $_.Trim() -ne "" } | ForEach-Object { $_.TrimEnd() } | Set-Content -Path "C:\Bijay\UpdatedSampleFile.txt"

In this version:

  1. Get-Content reads the content of the file and passes it down the pipeline.
  2. Where-Object filters out any lines that are empty or contain only whitespace characters using the Trim() method and the -ne (not equal to) operator.
  3. ForEach-Object applies the TrimEnd() method to each remaining line, removing any trailing whitespace characters.
  4. Set-Content writes the filtered and trimmed content back to the file.

This approach ensures that your file ends with the last line of actual content, without any empty lines or trailing whitespace.

Conclusion

In this tutorial, I have explained how to use various methods to remove the last empty line from a file using PowerShell. You can use a combination of cmdlets, regular expressions, or the Trim() method, etc.

I hope this tutorial has helped you understand the different approaches to removing the last empty line from a file using PowerShell. If you have any questions or suggestions, feel free to leave a comment below.

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.