To embed '
(single quotes) in a string literal in PowerShell, you have 3 options:
Inside single-quoted strings ('...'
), double them:
# ' doubled
'Honey, I''m home.'
Single-quoted strings are otherwise interpreted verbatim, so no other chars. need escaping.
Conversely, this means that no variable references or expressions are expanded (interpolated) in such strings; if that is needed - e.g., if $Job
in your query string is meant to refer to the value of a $Job
variable defined in PowerShell - you must use a double-quoted string ("..."
) - see below.
Inside single-quoted here-strings (@'<newline>...<newline>'@
), you don't need to escape '
at all.
This always multi-line variant of a single-quoted string requires no escaping of any characters.
# No escaping needed (ignore the broken syntax highlighting)
@'
Honey, I'm home.
'@
Note: The closing '@
must be at the very start of the line - not even preceding whitespace allowed.
Inside double-quoted strings ("..."
), you don't need to escape '
at all.
However, since such expandable strings (interpolating strings) by design interpret $
as the start of a variable reference (e.g., $var
) or subexpression (e.g. $(1+2)
), you'll then have to escape literal $
chars. as `$
`
(the backtick) generally serves as the escape character in PowerShell (see link below), so you would simililarly use `"
to escape embedded "
chars.
The need to escape literal $
chars. as `$
equally applies to the here-string variant of expandable strings
(@"<newline>...<newline>"@
), but embedded "
do not need escaping.
# No escaping of ', but literal $ must be escaped as `$
"Honey, I'm home; I hate my `$Job."
See also:
- about_Quoting_Rules, the official help topic on string literals.
- about_Special_Characters, which explains the
`
-prefixed escape sequences supported inside double-quoted strings, such as `t
to represent a tab character. Note that Windows PowerShell supports fewer sequences than PowerShell [Core] 6+.
- This answer, which explains expandable strings (string interpolation) in detail.