If you are working with strings, you will have a very common requirement of trimming a string in PowerShell. There are various methods to do this. In this tutorial, I will show you how to trim strings in PowerShell using various methods.
To trim strings in PowerShell, you can use methods like Trim(), TrimStart(), and TrimEnd(). These methods help remove unwanted characters from the beginning, end, or both ends of a string. For example, to remove leading and trailing spaces from a string, you can use:
$exampleString = " Hello, World! "
$trimmedString = $exampleString.Trim()
Write-Output $trimmedStringThis will output “Hello, World!” without the extra spaces.
In PowerShell, we trim a string to remove unwanted characters from the beginning, end, or both ends. This is often necessary before processing a string.
There are various methods to trim strings in PowerShell.
PowerShell Trim() Method
The Trim() method in PowerShell is used to remove all leading and trailing whitespace characters from a string. This method is part of the System.String class in .NET, which PowerShell leverages for string manipulation.
Syntax
The syntax for the Trim() method is:
$string.Trim()You can also specify characters to trim by passing them as arguments like below:
$string.Trim([char[]])This is one of PowerShell’s most used methods to trim a string.
Read Count the Number of Characters in a String in PowerShell
Methods to Trim Strings in PowerShell
Let us check all the methods for trimming a string in PowerShell with examples.
1. Trim() Method
As discussed above the Trim() method in PowerShell removes all leading and trailing whitespace characters from a string.
Example:
$exampleString = " Welcome to New York! "
$trimmedString = $exampleString.Trim()
Write-Output $trimmedStringOutput:
Welcome to New York!In the screenshot below, you can see that all the spaces from the beginning and end of the string were removed. I executed the above PowerShell script using VS code.

2. TrimStart() Method
Like the trim() method, the TrimStart() method in PowerShell removes all leading whitespace characters from a string. You can also specify which characters to remove.
Example:
$exampleString = " Welcome to New York!"
$trimmedStartString = $exampleString.TrimStart()
Write-Output $trimmedStartStringOutput:
Welcome to New York!After I executed the above PowerShell script, you can see the output in the below screenshot.

You can also specify characters to trim:
$exampleString = "###Welcome to New York!"
$trimmedStartString = $exampleString.TrimStart('#')
Write-Output $trimmedStartStringOutput:
Welcome to New York!Read Check if a String Contains a Substring in PowerShell
3. TrimEnd() Method
The TrimEnd() method in PowerShell removes all trailing whitespace characters from a string. Similar to TrimStart(), you can specify which characters to remove.
Example:
$exampleString = "Welcome to New York! "
$trimmedEndString = $exampleString.TrimEnd()
Write-Output $trimmedEndStringOutput:
Welcome to New York!You can also specify characters to trim:
$exampleString = "Welcome to New York!!!!"
$trimmedEndString = $exampleString.TrimEnd('!')
Write-Output $trimmedEndStringOutput:
Welcome to New YorkI executed the above PowerShell script, and you can see the output in the screenshot below:

4. Using -replace operator
If you need to remove specific patterns or characters from a string, use the -replace operator in PowerShell.
Example:
$exampleString = "123-456-7890"
$trimmedString = $exampleString -replace '-', ''
Write-Output $trimmedStringOutput:
1234567890Here is the output in the screenshot below, after I executed the above PowerShell script.

Conclusion
I hope you learn how to trim strings in PowerShell using various methods like Trim(), TrimStart(), and TrimEnd(), etc.
We also saw one example of how to trim a string in PowerShell using the -replace operator. Still, have questions? Feel free to leave a comment below.
You may like the following tutorials:
- Check if a String Contains Special Characters in PowerShell
- Convert Multiline String to Single Line in PowerShell
- Get the First and Last Line of a Multiline String in PowerShell
- Concatenate String and Integer in 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.