powershellcharacter-replacement

Powershell, replace DOT with SPACE


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?


Solution

  • -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.