Escape from Powershell Regex!

Alright, hopefully this will be a quick one.

 

Sometimes with powershell it takes strings of characters and even though you don’t want it to, it interprets them as Regular Expression(Regex).
When this happens, sometimes there is really just no way to get around powershell’s understanding of what you want….

original

 

As we see in this screenshot, I really just want to do a simple replace of two strings, but powershell thinks my “D:\” is a regular expression and not just a plain ole’ string.

 

When this happens there is much you can do….Except for one thing…

fix

 

In this example, instead of just saying “$path1” as I did in the first example. I added to it with “[regex]::Escape($path)”

Using this method tells powershell that whatever is in the () brackets is ABSOLUTELY NOT REGEX, so it understands my $path1 as just the string that it is.

 

Hopefully this Helps!

Leave a Reply