September 21st, 2014

PowerTip: Remove Leading and Trailing Spaces with PowerShell

Doctor Scripto
Scripter

Summary: Use the Trim() method to remove extraneous space from a String object.

Hey, Scripting Guy! Question Is there an easy way to drop all spaces that are before or after a string in Windows PowerShell?

Hey, Scripting Guy! Answer Use the Trim() method to remove all spaces before and after a string from the output, for example:

$Something='                 I love PowerShell                     '

$Something.trim()

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.