Often when using the -Replace (Operator) or .replace() (Method) in Powershell, I forget that the former parses the string in regex and in case you’re passing Special characters in the string, you’ll get the error all over your console.

That is because some special characters are part of regular expression language and are considered are Meta Characters in RegEx, so it’s always a best practice to escape special characters.

To your rescue,  here is a quick tip to escape all special characters in a string using the .Net Type accelerator for Regular expression [RegEx] , something like in the following image –

regex

and an animation with a use case.

tip

Hope you’ll find this useful, until next time, Cheers!

signature[twitter-follow screen_name=’SinghPrateik’]