Here are a few facts to note, we rarely have perfect data, and data files incorporate a lot of strings. When you write a script you would realize that more often than none, there is a need to format the data and get rid of irrelevant characters within our string blocks. This could be a tremendous amount of work, but thankfully, we have the PowerShell Trim() method.
This PowerShell Trim() function gives a lot of flexibility in the way strings can be handled and makes the process generally easier. Users can use variants of the PowerShell Trim() including the PowerShell Trimend() method.
I would be exploring with you in this article, how this function works and will be showing some everyday applications of the function.
Requirements for PowerShell Trim() Method
Before beginning this tutorial you must be aware that the scripts I run have been run on Powershell 7. They should work on earlier versions of PowerShell Core as well as earlier versions of Windows Powershell, but I recommend upgrading Windows Powershell 7.
How the Powershell Trim Method is Used
The trim() function is one of the most popular methods used for trimming strings in PowerShell. The trim() function, like all other trimming functions in PowerShell, is a System.String A .NET class member. This function can be applied in removing every whitespace at the beginning and ends of strings, as well as to remove specific characters.
Important note: The Trim method is case-sensitive.
How to Trim White Spaces
Admins can use the trim() function with no parameters to remove whitespace from strings, as seen below.
When the Trim() function in PowerShell is invoked without any arguments, the Char.IsWhiteSpace method is used to get rid of any beginning and ending whitespaces in the script.
# The aim here is to get rid of both the trailing and leading spaces. (" Test ").Trim() # The aim is to get rid of the two leading spaces as well as the newline chracter and carriage return. (" Test`r`n").Trim() # The aim is to get rid of the two leading spaces and following Unicode chracter for Next Line. (" Test Test $([Char]0x0085)").Trim()
Good to know:
The white-space character list in.NET Framework 3.5 SP1 and earlier versions was different from the one in.NET Framework 4 and recent versions. This implies that both ZERO WIDTH NO-BREAK SPACE (U+FEFF) and ZERO WIDTH SPACE (U+200B) characters were deleted in earlier releases of .NET, but not in subsequent ones.
NARROW NO-BREAK SPACE (U+202F), MONGOLIAN VOWEL SEPARATOR (U+180E), and MEDIUM MATHEMATICAL SPACE (U+205F) are 3 Unicode white-space characters that are not removed by the Trim() function in older versions of.NET. In subsequent versions of.NET, they are deleted.
How to Use Powershell Trim on Single Character
The trim technique may be used to remove single characters from a string. In all cases, until it finds a character other than the one given, this function removes that character from the beginning and end of a string.
I have written a script sample below for trimming the letters T and t.
# Aimed at getting rid of nothing, in this it leads with a space chracter and ends with a lower "t". (" Tests Afam Test").Trim("T") # Aimed at getting rid of the leading "T" the lower case "t" would be untouched. ("Test Afam Test").Trim("T") # Aimed at getting rid of nothing since it leads with an uppercase "T" and no ending "t". ("Test Afam String").Trim("t") # Aimed at getting rid of both leading and ending "t". ("test Afam test").Trim("t")
How to Use Powershell Trim on an Array of Characters
Getting Rid of a Long Array of Characters
In some cases, you may want to remove several characters from a string. You can furnish an array of characters to the PowerShell trim() function.
When you provide an array of characters to this function, it will remove all of those characters from the start and the end of a string object until it runs across one that isn’t in the array.
The trim() function takes an array as input, which in somecases is a simple string like add2123 or a real array like @. (“A”,”D”,”D”,”2″,”1″,”2″,”3″).
You should note that using the trim() function on an array may result in some surprising results.
# Aimed to get rid of both leading and trailing "ABC1123" strings. ("ABC1123TestABC123TestABC1123").Trim("ABC1123") # Aimed to get rid of both leading and trailing "ABC1123" strings. ("ABC1123TestABC123TestABC1123").Trim(@("A","B","C","1","1","2","3")) # Aimed to get rid of the leading "ABC1123" string and the trailing "ABC1123" string. ("ABC1123Test123TestABC123TestABCTestABC1123").Trim("ABC1123")
Even in cases of characters being put out of order, as seen below, they are still eliminated since they match one of the characters in the supplied array. This technique will not work if you want the precise text to be deleted from both the beginning and end of the string.
How to Use TrimEnd() and TrimStart() Methods
If you don’t want to trim characters from the beginning and end of a string, you may use either the PowerShell TrimEnd() or TrimStart() functions to be more selective. The behavior of these methods is identical to that of the PowerShell Trim() technique.
Note that these two functions are case-sensitive techniques!
TrimStart() and TrimEnd() both allow users to specify the part of a string object they want to operate on. These techniques may be more suited to your data requirements but would depend on individual circumstances.
Below are some sample scripts I have provided:
# Aimed at removing the leading spaces and keeping the following newline and carriage return characters. (" Test`n`r").TrimStart() # Aimimed at removing the 2 leading "b" characters and leaving the following "b" characters. ("bbTestbb").TrimStart("b") # Aimed at removing the initial "abcc1123" and leaving the final "abcc1123" characters. ("abcc1123Testabc123").TrimStart("abcc1123") # Aimed at leaving the initial two spaces and getting rid of the following newline and carriage return characters. (" Test`n`r").TrimEnd() # Aimed at leaving the initial "b" characters and getting rid of the final "b" characters. ("bbTestbb").TrimEnd("b") # Aimed at leaving the initial "abcc1123" characters and getting rid of the final "abcc1123" characters. ("abcc1123Testabc123").TrimEnd("abcc1123")
More Trim Methods in Powershell
Additional Trim Methods in PowerShell
Trimming strings is not different from everything else in PowerShell; there are numerous methods to accomplish just about anything. To delete a specific string, instead of utilizing the PowerShell trim() methods, you may utilize the IndexOf() and SubString() methods of a string object.
Consider the following comparison between TrimEnd() and the IndexOf() and SubString() methods.
# Aimed at getting rid of "TeTest" and not just the "Test" in the string. ("StringTeTest").TrimEnd("Test") # Aimed to get rid of just "Test" in the string. ("StringTeTest").Substring(0,"StringTeTest".IndexOf("Test"))
The SubString function is a little more difficult to use. Users may keep the remaining string content while only deleting the specified string by finding the index of the Test string from inside the string object and just returning characters from the beginning of the string to that point.
Final Thoughts on PowerShell Trim
Trim(), TrimEnd(), and TrimStart() are string functions in PowerShell that may be used to tidy up data as required. If any of these 3 aren’t sufficient, PowerShell offers a range of alternatives that can be applied with varying degrees of control. Apply these methods to learn how to cut strings in PowerShell now!