In Powershell, how can I replace a string that contains a question mark?

Question:

In Powershell, how can I replace a string that contains a question mark? For example:

(Get-Content file.txt) -replace "Hello?","Hello."

The question mark seems to be interpreted as some kind special character. Is there a way to escape it? I tried using one or two backticks, but no success.

Answer:

The -replace operator uses Regular Expression pattern matching. In RegEx the question mark is a quantifier indicating the previous match should be matched zero or one times. You can escape the question mark by placing a backslash before it as such:

Source:

In Powershell, how can I replace a string that contains a question mark? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply