Removing more than one white space in powershell

If you're looking to collapse multiple consecutive whitespace characters into a single space then you can do this using the -replace operator. Given...

PS> $beforeReplace = '   [   Hello,   World!   ]   '
PS> $beforeReplace
   [   Hello,   World!   ]   
PS> $beforeReplace.Length
29

...you would call the -replace operator like this...

PS> $afterReplace = $beforeReplace -replace '\s+', ' '
PS> $afterReplace
 [ Hello, World! ] 
PS> $afterReplace.Length
19

The first parameter to -replace is a regular expression pattern to match, and the second parameter is the text that will replace any matches. \s will match a whitespace character, and + indicates to match one or more occurrences, so, in other words, one or more adjacent whitespace characters will be replaced with a single space.

Replacement without whitespace normalization

If you don't need to normalize all whitespace characters to spaces and, thus, it's ok for standalone whitespace characters to be left untouched, then for long strings you might see better performance with this variation...

PS> $afterReplace = $beforeReplace -replace '\s{2,}', ' '
PS> $afterReplace
 [ Hello, World! ] 
PS> $afterReplace.Length
19

The \s{2,} uses a quantifier meaning "match the preceding element at least two times"; therefore, standalone whitespace characters will not be replaced. When the input string contains a mix of whitespace characters...

PS> $beforeReplace = "1Space: ;2Space:  ;1Tab:`t;2Tab:`t`t;1Newline:`n;2Newline:`n`n;"
PS> $beforeReplace
1Space: ;2Space:  ;1Tab:    ;2Tab:      ;1Newline:
;2Newline:

;
PS> $beforeReplace.Length
57

...note how the results for the two approaches differ...

PS> $afterReplaceNormalized = $beforeReplace -replace '\s+', ' '
PS> $afterReplaceNormalized
1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline: ;2Newline: ;
PS> $afterReplaceNormalized.Length
54
PS> $afterReplaceUnnormalized = $beforeReplace -replace '\s{2,}', ' '
PS> $afterReplaceUnnormalized
1Space: ;2Space: ;1Tab: ;2Tab: ;1Newline:
;2Newline: ;
PS> $afterReplaceUnnormalized.Length
54

While both yield strings of the same length, the unnormalized replacement leaves the single space, single tab, and single newline whitespace runs unmodified. This would work just the same whether adjacent whitespace characters are identical or not.

Additional documentation

  • Enter help about_Comparison_Operators [ Windows PowerShell 2.0 ][ PowerShell (Core) ]
  • Enter help about_Regular_Expressions [ Windows PowerShell 2.0 ][ PowerShell (Core) ]
  • .NET Regular Expression Language - Quick Reference