4

Following keyword need to be searched on document using PowerShell:

["Allowed Acquisition Clean-Up Period" 
$keyword = ""
Get-Content $SourceFileName | Select-String -Pattern $keyword

String which I'm searching for has double quotes in it, so I'm struggling how to mention in this $keyword.

1
  • 2
    Just a tip: You can use the [regex]::Escape() method if you're unsure about what to escape in the future. Aug 5, 2018 at 14:57

2 Answers 2

8

Apparently you not only have double quotes, but also an opening square bracket. Square brackets are meta-characters in regular expressions (for defining character classes), so you need to escape those too.

Define your expression in single quotes:

$keyword = '\["Allowed Acquisition Clean-Up Period"'

or escape nested double quotes with backticks:

$keyword = "\[`"Allowed Acquisition Clean-Up Period`""
2
  • above doesn't work with LIKE clause $SearchKeyword1 = '["Lookup keyword"' if ($para.Range.Text -Like $SearchKeyword1) Aug 6, 2018 at 14:52
  • 2
    @user3657339 Of course it doesn't. Regular expression matches (-match, Select-String) and wildcard matches (-like) are not the same and do not use the same patterns. Aug 6, 2018 at 15:02
3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.