To complement Ansgar Wiechers' helpful answer, which contains the correct solution:
Given that "
is not a regex metacharacter (has no special meaning in a regular expression), your question boils down to:
How can I embed a "
(double quote) in a string in PowerShell?.
- As an aside: As stated,
[
is a regex metacharacter, so it must be escaped as \[
inside a regex in order to be treated as a literal. As TheIncorrigible1 points out, you can let [regex]::Escape($string)
handle that escaping for you; the result treats the content of $string
literally in the context of a regex.
There are several options, demonstrated here with a simplified sample string, 3 " of rain
- see also: Get-Help about_Quoting_Rules
:
# Inside a *literal string* ('...'):
# The content of single-quoted strings is treated *literally*.
# Double quotes can be embedded as-is.
'3 " of rain'
# Inside an *expandable string* ("..."):
# Such double-quoted strings are subject to *expansion* (interpolation)
# of embedded variable references ("$var") and expressions ("$(Get-Date)")
# Use `" inside double quotes; ` is PowerShell's escape character.
"3 `" of rain" #"
# Inside "...", "" works too.
"3 "" of rain"
# Inside a *literal here-string* (multiline; end delimiter MUST be
# at the very beginning of a line):
# " can be embedded as-is.
@'
3 " of rain
'@
# Inside an *expanding here-string*:
# " can be embedded as-is, too.
@"
3 " of rain
"@
For the sake of completeness: you can create a double quote via its Unicode code point (the number that identifies each character), which is 0x22
(hex) / 34
(decimal), by casting it to [char]
, e.g.: [char] 0x22
.
You can use this:
- in string concatenations:
'3 ' + [char] 0x22 + ' of rain'
- in string-formatting expressions with the
-f
operator: '3 {0} of rain' -f [char] 0x22