How to Remove Whitespace from Strings in PowerShell?

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 $cleanedFilename

Output:

report2024.pdf

The output is in the screenshot below after I executed the above PowerScript using VS code.

Remove Whitespace from Strings in PowerShell

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 $cleanedString

Output:

Indented text

The output is in the screenshot below after I executed the PowerShell script above.

powershell remove whitespace from string

Similarly, for trailing spaces:

$trailingSpacesString = "Text with trailing spaces    "
$cleanedString = $trailingSpacesString.TrimEnd()
Write-Output $cleanedString

Output:

Text with trailing spaces

Read 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 $cleanedString

Output:

Thisisastringwithspaces,tabs,andnewlines.

Here is the output in the screenshot below:

powershell remove spaces from string

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 $cleanedData

Output:

John,Doe,123MainSt,Anytown,USA

Here is the output in the screenshot below:

how to remove spaces from string in powershell

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 $cleanedInput

Output:

JohnDoe

Conclusion

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.