Powershell, replace DOT with SPACE

user6496229 picture user6496229 · Mar 28, 2017 · Viewed 17.7k times · Source

This works:

$string = "This string, has a, lot, of commas in, it."
  $string -replace ',',''

Output: This string has a lot of commas in it.

But this doesn't work:

$string = "This string. has a. lot. of dots in. it."
  $string -replace '.',''

Output: blank.

Why?

Answer

Jeff Zeitlin picture Jeff Zeitlin · Mar 28, 2017

-replace searches using regular expressions (regexp), and in regexps the dot is a special character. Escape it using '\', and it should work. See Get-Help about_Regular_Expressions.