I am trying to replace a sentence in .config file using powershell.
${c:Web.config} = ${c:Web.config} -replace
\'$BASE_PATH
$\\
Just to provide a bit more background to stej's answer, there are two things going on here:
1) While parsing the command, powershell is expanding the variables in the string arguments to -replace
(for example, the string "shell: $ShellId"
will expand the ShellId
variable, producing shell: Microsoft.PowerShell
). Using the backtick escape character, or declaring the string with single quotes, prevents this (both "shell: `$ShellId"
and 'shell: $ShellId'
become shell: $ShellId
).
2) The -replace
operator uses .NET regular expressions, where the $
, \
, and .
characters are special language elements. Using the backslash escape character allows special characters to be treated as literal values within the regular expression (e.g. \$
will match a dollar character, while $
will match the end of the line). Since $
is used by both powershell and regular expressions, it has to be escaped twice (using either "\`$"
or '\$'
).
In this case, another alternative would be to use the string.Replace method, which will perform a case-sensitive replacement:
${c:Web.config}.Replace(
'$BASE_PATH$\Test\bin$Test_TYPE$\WebTest.dll',
'c:\program Files\example\webtest.dll'
)