How to Trim Strings in PowerShell?

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

This 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 $trimmedString

Output:

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.

Trim Strings in PowerShell

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

Output:

Welcome to New York!

After I executed the above PowerShell script, you can see the output in the below screenshot.

trim a string in PowerShell

You can also specify characters to trim:

$exampleString = "###Welcome to New York!"
$trimmedStartString = $exampleString.TrimStart('#')
Write-Output $trimmedStartString

Output:

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

Output:

Welcome to New York!

You can also specify characters to trim:

$exampleString = "Welcome to New York!!!!"
$trimmedEndString = $exampleString.TrimEnd('!')
Write-Output $trimmedEndString

Output:

Welcome to New York

I executed the above PowerShell script, and you can see the output in the screenshot below:

How to Trim Strings in PowerShell

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

Output:

1234567890

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

PowerShell trim strings

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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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