Powershell

How to use PowerShell Trim method

PowerShell supports a variety of methods, functions, and cmdlets to perform several tasks automatically. The Trim() method is utilized to remove white space(s) or character(s) from the start/end of a string. The trimming methods of PowerShell belong to the “.Net” class of “System.String” and so does the Trim() method. Therefore, the Trim() method can only be applied to strings. Using TrimEnd() or TrimStart(), the trim method can be targeted at either the end of the beginning of the string. This write-up explains the application of Trim() methods to avoid any white spaces and characters.

How does PowerShell’s Trim() method work

The building block of Trim() and its derivative methods depends on the syntaxes provided below:

“string”.Trim() or $string.Trim(): The Trim() method removes the white space or the empty new line from the end and start of the string. The string can be passed directly to the Trim() method or it can be passed as a variable as well.

“string”.Trim(“char”) or $string.Trim(“char”): This syntax will remove the character specified in the parenthesis of the Trim() method.

“string”.TrimStart() or $string.TrimStart(): The syntax provided here trims the whitespaces/characters from the start of string.

“string”.TrimEnd() or $string.TrimEnd(): Like TrimStart(), this method trims the string values from the endpoint of a string.

How to use the Trim() Method in PowerShell

Here, you will learn to apply the Trim() method and its derivatives in multiple contexts.

Using the Trim() Method to remove white spaces in PowerShell

The following command passes a string directory to the Trim() method. If nothing is passed as an argument in the Trim() method then it would only remove the white spaces (if they exist) at the start and end of the string. From the input, it can be observed that two white spaces are there at the start and end of the string. After applying the Trim() method, all the white spaces are removed and a more refined string is obtained.

> " Linuxhint provides quality content!! ".Trim()

Graphical user interface, text Description automatically generated with medium confidence

Apart from directly passing the string, the following commands store a string in a variable ($str) and then apply the Trim() method to that variable.

> $str=" Linuxhint provides quality content!! "

> $str

> $str.Trim()

Text Description automatically generated

Using Trim() to remove a character in PowerShell

In the below-stated command, the Trim() method is passed on a string value and it removes the “z” character occurring at either end of the string. It is concluded from the output that Trim() vanishes all the “z” characters regardless of the number of occurrences. However, the command provided below will not remove the white spaces and thus the “z” occurring after the white spaces will not be removed.

> " zlinuxhint zz".Trim("z")

In order to remove the spaces along with the character, you have to add space before and after that character as described below.

> " zlinuxhint zz".Trim(" z ")

Using Trim() to remove multiple characters in PowerShell

Moreover, multiple characters can also be removed using the Trim() method. The command provided below trims the string object by passing characters directly or as an array. The command written below removes the character “l1i2n3u4x” occurring at the start or end of the string object. Interestingly, the output shows that the ordering of characters does not matter, what matters is the characters.

> "linux1234 PowerShell 12linux34".Trim("l1i2n3u4x")

Graphical user interface Description automatically generated

Using TrimStart() in PowerShell

As the name of this method directs, it would function on the initial(start) portion of the string. For instance, the command provided below will only deal with whitespaces that occur at the start of the string.

> " You are working on PowerShell!! ".TrimStart()

Graphical user interface, website Description automatically generated

Moreover, any character can be removed from the start by specifying the character in parenthesis as described in the command below. The output shows that only that “!” is deleted which occurs at the start of the string.

> "!!You are working on PowerShell!!".TrimStart("!")

Using TrimEnd() in PowerShell

The TrimEnd() method targets the ending part of the string and removes characters or white spaces from the end. To show its practicality, we have exercised the command written below that removes the characters from the end of the string.

> "PowerShell linuxhint PowerShell".TrimEnd("PowerShell")

A picture containing logo Description automatically generated

Conclusion

The PowerShell Trim() method functions to remove the white spaces or the characters from the start or end of the string. The trimming support of PowerShell includes TrimStart() and TrimEnd() methods as well. This article demonstrates how to clean up the string by removing white spaces and characters using several Trim() methods. The Trim() and its child methods are case sensitive and in such conditions Trim() only removes the characters that match the case. So, if you encounter string objects frequently, start trimming them for better understanding.

About the author

Adnan Shabbir