Replace Escape

by Jun 24, 2013

There are two ways for replacing text, and both have their advantages and pitfalls:

Each string has a Replace() method which works very straightforward:

Unfortunately, as you can see, the method is case-sensitive, and there is no way to change that.

Alternatively, you can use the -replace operator which is not case sensitive:

However, the operator -replace really takes a regular expression, and if your replace word contains one of the reserved regex keywords, all breaks or produces wrong results:

The "." really is a placeholder that represents any character which is why -replace also replaced the beginning of the file name.

To make -replace a simple operator that only takes plain text, simply escape your text like this:

Once you do that, you can safely use -replace without having to worry about regex specialties.

Twitter This Tip! ReTweet this Tip!