Skip to content

Remove spaces from CSV/TXT file with PowerShell

How can we remove whitespace, leading spaces, and trailing spaces from CSV/TXT file? Before we explain that, why do we want to do that? The reason is that a PowerShell script is not reading the values from the CSV/TXT file because of the whitespace. In this article, you will learn how to remove spaces from CSV/TXT file with PowerShell.

Different spaces in CSV/TXT file

Understand the difference:

  • Whitespace refers to any space character in a text string, including spaces, tabs, and line breaks.
  • Leading spaces are the whitespace characters that occur at the beginning of a text string.
  • Trailing spaces are the whitespace characters that occur at the end of a text string.

Remove space from CSV file with PowerShell

Use PowerShell to remove whitespace, leading spaces, and trailing spaces from CSV file.

Remove whitespace from CSV file

Remove any space characters in the CSV file.

(Get-Content -Path "C:\temp\Users.csv" | foreach {$_ -replace '\s+', ''}) | Set-Content -Path "C:\temp\UsersWS.csv"

Remove leading spaces from CSV file

Remove space characters at the front of each line in the CSV file.

(Get-Content -Path "C:\temp\Users.csv" | foreach {$_ -replace '^\s+', ''}) | Set-Content -Path "C:\temp\UsersLS.csv"

Remove trailing spaces from CSV file

Remove space characters at the end of each line in the CSV file.

(Get-Content -Path "C:\temp\Users.csv" | foreach {$_ -replace '\s+$', ''}) | Set-Content -Path "C:\temp\UsersTS.csv"

Remove space from TXT file

Use PowerShell to remove whitespace, leading spaces, and trailing spaces from TXT file.

Remove whitespace from TXT file

Remove any space characters in the text file.

(Get-Content -Path "C:\temp\Users.txt" | foreach {$_ -replace '\s+', ''}) | Set-Content -Path "C:\temp\UsersWS.txt"

Remove leading spaces from TXT file

Remove space characters at the front of each line in the text file.

(Get-Content -Path "C:\temp\Users.txt" | foreach {$_ -replace '^\s+', ''}) | Set-Content -Path "C:\temp\UsersLS.txt"

Remove trailing spaces from TXT file

Remove space characters at the end of each line in the text file.

(Get-Content -Path "C:\temp\Users.txt" | foreach {$_ -replace '\s+$', ''}) | Set-Content -Path "C:\temp\UsersTS.txt"

That’s it!

Read more: Import CSV delimiter PowerShell »

Conclusion

You learned how to remove whitespace from CSV/TXT file with PowerShell. When you have to remove whitespace, leading spaces, or trailing spaces, use one of the commands to get the job done.

Did you enjoy this article? You may also like PowerShell remove quotation marks from a text file. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has One Comment

  1. The code for the CSV and TXT files seem to be the same thing. I’m struggling a bit with doing this for a CSV file as it has commas as delimiters.

Leave a Reply

Your email address will not be published. Required fields are marked *