Posted By

ericmorin21 on 04/24/09


replace space spaces PowerShell

Versions (?)

Who likes this?

3 people have marked this snippet as a favorite


Powershell - Replace multiple spaces with single spaces

 / Published in: Windows PowerShell

I needed to clean up strings with an unpredictable number of spaces interspersed throughout. Powershell's Replace method to the rescue.

  1. $badString = "This is not the way it should be."
  2. $badString #for debug only
  3. while ($badString.Contains(" ")){
  4. $badString = $badString -replace " "," "
  5. }
  6. $badString

Report this snippet  


RSS Icon Subscribe to comments
Posted By: genericpenguin on October 27, 2010

Maybe a regular expression might be help.

$string = "This is a string with lots of extraneous spaces." $string -replace '\s+', " "

The \s represents a whitespace character (space or tab) and the plus sign means one or more instances.

Just a thought.

You need to login to post a comment.