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:
Get-Contentreads the content of the file and passes it down the pipeline.Where-Objectfilters out any lines that are empty or contain only whitespace characters using theTrim()method and the-ne(not equal to) operator.Set-Contentwrites 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.

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:
Get-Contentreads the content of the file as a single string using the-Rawparameter.- 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. - The
-replaceoperator replaces the matched line breaks with an empty string, effectively removing the last empty line. Set-Contentwrites 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:
Get-Contentreads the content of the file as a single string using the-Rawparameter.- The
Trim()method removes any whitespace characters, including line breaks, from the beginning and end of the string. Set-Contentwrites 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:
Get-Contentreads the content of the file and passes it down the pipeline.Where-Objectfilters out any lines that are empty or contain only whitespace characters using theTrim()method and the-ne(not equal to) operator.ForEach-Objectapplies theTrimEnd()method to each remaining line, removing any trailing whitespace characters.Set-Contentwrites 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:
- Create JSON Files with Content Using PowerShell
- Remove Blank Lines from PowerShell Format-Table Output
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.