Monday, January 23, 2012

Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods


Did you ever tried to use .Trim() method on a string and can’t get it working? if yes ? Then welcome to the novice group. and if no? man you are genius.

Kidding :)

i was looking for some example for it i found few but not with screenshots :) .. then i decide to write a article on it … 

So lets start.

I am using the text “The Quick brown fox jumps over the lazy dog” and put this text in to a variable $text
$text = "    The Quick brown fox jumps over the lazy dog.     "

You can see that i have a space in the the beginning of the text and the end of the text.

23-01-2012 16-26-58

Lets remove these spaces to remove these spaces we are going to use trim() method.

The Trim() remove the black characters to the right and left 


You can see that white space is removed from beginning of “the quick brown”

23-01-2012 16-35-10


The TrimStart() remove all the chracters from the start of the string.

If you want to remove any from the beginning of the text than we need to use TrimStart() method. Lets remove “The QUick” from the text.

$text.TrimStart("The Quick")

Now you can see in the output “The QUick” is removed from output.

23-01-2012 16-40-41

not lets trim the end of the string using TrimEnd() method


The TrimEnd() remove are the characters from the string.

$text.TrimEnd("the lazy dog.     ")

You can see that now “the lazy dog.” is removed from the output.

23-01-2012 17-37-57

I hope this helps someone…

Aman Dhally


  1. For me it was a gret help. Thank you very much


  2. Hi,

    You are welcome Nobert, i am glad that you find it helpful.


  3. Hey Aman,

    thank you for your description. You inspired me to write an article about Trim() many other functions in PowerShell, e.g. Substring(), Contains(), ToLower(), ToUpper(), StartsWith(), EndsWith().

    My article is in German. Here is the link:

    Thank you


    1. Hi Deer,

      You are welcome and thanks for writing the same for your native language, I hope lots of users get benifitts from it.

      Keep it up the good and noble work.

      Aman Dhally

  4. Thank you for writing this! It's going to help a lot in my project deployment.

  5. Hey Aman,

    The TrimEnd and TrimStart examples are misleading. Consider the following statement:

    "The Quick brown fox jumps over the lazy dog".TrimEnd("dgo")

    This will trim "dog" from the end of the string. TrimStart and TrimEnd remove any consecutive concurrence of characters from the start or end from the string that are present in their parameter. This behavior can cause unexpected results in the following example:

    "The Quick brown fox jumps over the good dog".TrimEnd(" dog")

    You will find that " good dog" is trimmed.

    1. Hi Jan,

      You are right. Thanks for correcting me, I will update the post soon :)

      Thanks a ton.


  6. Hi Aman! Excellent post, it helped me allot. I just have a quick question. how can I make the trimend method case-insensitive?

  7. Thanks for this. I have found a number of interesting articles now on this topic, but they are all about editing a predetermined string.

    I have been trying to figure out how to edit filenames using trim. We have a number of files with starting spaces that I'd like to get rid of.
    Any pointers, please? :-D
    I can't even figure what to google for :-/

  8. Dear Aman, Thans for the great job explaining TrimStart. $text1.TrimStart("X-SKYPE-PSTNNUMBER:") >>zz1.txt worked like a charm:-)

    I have Skype VCF file and trying to get only name and phone number. For some reason I get error when using $text1.Substring(19,12) or $text1.Substring(19,$text1.length-19) - any tips would greatly appresicated. DD

  9. I'm turning around on this and nothing found as yet for v5. Most of this .something on a string seems to have disappeared and no trace for an equivalent other than Select-Strung...
    Ideas anyone?

    1. After failing to find official info, I have found that the variable type generated by Select-String would return an object type 'Matchinfo' (?). I have explicitly declared the variable holding the result as [string]
      Use the $foo.GetType build-in function
      I have found all the built-in functions for a string variable
      Hope this helps someone
      ps5 powershell version 5 string manipulation

  10. Hi,I am stuck at one point.The below trim command works at times and sometimes not!
    It is used to format the output csv file into two different columns and remove the semicolon
    (gc $FILENAME) | ? {$_.trimstart() -ne "" } | set-content $FILENAME

    Current output is as below which should remove the semicolons and format the output separately in two columns.Unfortunately its not working now!!!! :(((
    "d0039r00 ;OPEN"
    "s0079r00 ;OPEN"
    "dbsla1 ;OPEN"
    "dbslb1 ;OPEN"
    "sbsla1 ;OPEN"
    "sbslb1 ;OPEN"
    "sbslc1 ;OPEN"
    "sbsld1 ;OPEN"
    "sbsle1 ;OPEN"


Note: Only a member of this blog may post a comment.