While working with strings, I got a requirement to remove whitespace from strings in PowerShell. In this tutorial, I will explain several methods to remove whitespace from strings in PowerShell with some real examples.
To remove leading and trailing whitespace from a string in PowerShell, you can use the Trim() method. This method cleans up any extraneous spaces at the beginning and end of a string. For example, if you have a string $filename = " report2024.pdf ", using $cleanedFilename = $filename.Trim() will result in "report2024.pdf", removing all the unnecessary spaces around the filename.
Remove Whitespace from Strings in PowerShell
Let me show each method with examples of removing whitespace from strings in PowerShell.
1. Using the Trim() Method
The Trim() method is the best way to remove leading and trailing whitespace from a string in PowerShell. This method does not affect whitespace inside the string.
Example:
Suppose you are processing a list of filenames padded with spaces at the beginning and end.
$filename = " report2024.pdf "
$cleanedFilename = $filename.Trim()
Write-Output $cleanedFilenameOutput:
report2024.pdfThe output is in the screenshot below after I executed the above PowerScript using VS code.

Check out Check if a String Contains a Space in PowerShell
2. Using the TrimStart() and TrimEnd() Methods
If you need to remove whitespace only from the beginning or the end of a string, you can use TrimStart() or TrimEnd() respectively. Let me show you an example.
Example:
Suppose you are dealing with a string where leading spaces are used as indentation and you want to remove them. Here is the complete script.
$indentedString = " Indented text"
$cleanedString = $indentedString.TrimStart()
Write-Output $cleanedStringOutput:
Indented textThe output is in the screenshot below after I executed the PowerShell script above.

Similarly, for trailing spaces:
$trailingSpacesString = "Text with trailing spaces "
$cleanedString = $trailingSpacesString.TrimEnd()
Write-Output $cleanedStringOutput:
Text with trailing spacesRead Remove Newline from String in PowerShell
3. Using the -replace Operator
The -replace operator in PowerShell is very useful for removing all whitespace characters, including spaces, tabs, and newlines, from a string.
Example:
Consider a scenario in which you have a string with multiple types of whitespace and want to remove all of them. Here is the complete script.
$messyString = "This is a string with spaces, tabs\t, and newlines\n."
$cleanedString = $messyString -replace "\s", ""
Write-Output $cleanedStringOutput:
Thisisastringwithspaces,tabs,andnewlines.Here is the output in the screenshot below:

4. Using the Replace() Method
If you only need to remove spaces (and not other types of whitespace), you can use the Replace() method in PowerShell.
Example:
Imagine you have a CSV file where spaces have accidentally been included in the data fields.
$csvData = "John, Doe, 123 Main St, Anytown, USA"
$cleanedData = $csvData.Replace(" ", "")
Write-Output $cleanedDataOutput:
John,Doe,123MainSt,Anytown,USAHere is the output in the screenshot below:

Read PowerShell String Comparison: Case-Sensitive vs Case-Insensitive
5. Combine Methods for Complex Scenarios
Sometimes, you might need to combine these methods to achieve the desired result. For instance, you may want to remove leading and trailing whitespace first and then remove any remaining spaces within the string in PowerShell.
Let me show you an example.
Example:
Suppose you are cleaning up user input from a web form where users might have entered extra spaces.
$userInput = " John Doe "
$cleanedInput = $userInput.Trim().Replace(" ", "")
Write-Output $cleanedInputOutput:
JohnDoeConclusion
In this tutorial, I have explained various methods of removing whitespace from strings in PowerShell, such as using the Trim(), TrimStart(), and TrimEnd() methods. You can also use the -replace operator and Replace() method for removing all whitespace or specific characters within the string in PowerShell.
You may also like:
- Replace Multiple Strings in a File Using PowerShell
- Convert a String to a Binary Number in PowerShell
- How to Replace Text in Strings Using PowerShell?
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.