PowerTip: Remove Spaces from Both Ends of String

Doctor Scripto

Summary: Learn to use Windows PowerShell to remove spaces from both ends of a string.

Hey, Scripting Guy! Question How can I use Windows PowerShell to remove spaces at both ends of a string?

Hey, Scripting Guy! Answer Use the Trim method from the System.String .NET Framework class. 
          In this example, I create a string with a space at each end of the word, then
          I call the Trim method to remove both spaces:

$string = " a String "


Note  The Trim method exists on strings, so to use it, simply call it directly from the string. 


Discussion is closed.

Feedback usabilla icon