powershell

How to use PowerShell to remove space from the end of a line in a text file


I found a find-and-replace text answer from another question.

How do I use PowerShell to remove extra spaces at end of line in a text file?


Solution

  • There are a number of approaches but this one is pretty simple:

    $content = Get-Content file.txt
    $content | Foreach {$_.TrimEnd()} | Set-Content file.txt
    

    You may need to tweak the Encoding parameter on the Set-Content cmdlet to get the file output in the encoding you want (Unicode, ASCII, UTF8, etc).