Removing Multiple White Spaces

by | February 2,2012

Table of Contents

Removing multiple white spaces from text is easy in PowerShell.

-replace operator

Simply use -replace operator and look for whitespaces (“\s”) that occur one or more time (“+”), then replace them all with just one whitespace:

PS> '[     Man,     it works!     ]' -replace '\s+', ' '
[ Man, it works! ]

 
ReTweet this Tip!