PowerTip: Remove First Two Letters of String

PowerTip: Remove First Two Letters of String

  • Comments 10
  • Likes

Summary: Learn how to use Windows PowerShell remove the first two letters from a string.

Hey, Scripting Guy! Question How can I use Windows PowerShell to remove the first two letters from a string?

Hey, Scripting Guy! Answer Use the SubString method, for example:

$a = “String”

$a.substring(2) 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Should it not be $a.substring(0,2) ?

  • "{0}{1}" -f $a[0],$a[1]

  • Did you test this solution?

  • Gotta agree with the other comments here :)

  • @all Sorry, I was typing faster than I was thinking. The code above is correct, and yes, I did test it. What I did not do was proof read very well. I wanted to REMOVE the first two letters, which is what the code does. Sorry for the confusion. I have updated the posting now to reflect my actual intent. Thanks for pointing this out to me.

  • The subject line also needs to be modified to be consistent with the rest of the article.

  • No, thank you, it was fun to find a typo. :) Might I suggest a "What's wrong with this command" series? It might be fun to work on a PowerShell bug or two and learn something in the process.

  • @tester thanks, I just modified the title as well.

  • thank you