4

In powershell, double-quotes " indicate an "expandable" string, akin to "string interpolation" in other contexts. Single-quotes ' are strings which do not undergo interpolation.

I am a fan of this distinction, as it helps me anticipate how a particular string is being used.

however

Per these MS docs, Escape sequences are only interpreted when contained in double-quoted (") strings.

This seems to imply that I can't put a newline in a single-quoted string ?

orion elenzil
  • 4,484
  • 3
  • 37
  • 49
  • 2
    Can you add some code to your question ? What have you tried. – dcaz Dec 07 '21 at 17:21
  • Good point! But shoot, I only saw this comment now, and no longer have powershell in my day-to-day. If a powershell instance comes my way, I'll try to amend the question. – orion elenzil Jan 11 '23 at 17:21

1 Answers1

5

Indeed, PowerShell's verbatim strings ('...', i.e. single-quoted) support only one escape sequence: '', which allows you to embed a verbatim '

Therefore, your choices are:

  • Either: Embed a verbatim (actual) newline in your '...' string.

  • Or - and this applies to any character you want to generate programmatically rather than verbatim - use string concatenation (+) or a templating approach via -f, the format operator

The following examples show these techniques:

'--- verbatim'
# Note: If the enclosing script uses Windows-style CRLF newlines,
#       so will this string ("`r`n").
'line1
line2'

'--- concatenation'
# Note: In lieu of "`n" you may use [char] 10
#       For Windows-style CRLF newlines, use "`r`n" or + [char] 13 + [char] 10
'line1' + "`n" + 'line2'

'--- -f operator:
'line1{0}line2' -f "`n"

If embedding verbatim newlines is an option, you may also use a verbatim here-string for better readability, which has the added advantage of not having to escape embedded ' characters:

# Here-string
@'
line1
line2
No escaping of ' needed.
'@

As with regular verbatim strings, it is the newline format of the enclosing script (Windows-style CRLF vs. Unix-style LF) that determines the format of the newlines used in the string.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    thanks for the confirmation and paths forward. In my case using an actual newline in the string declaration works. It would be wonderful if the strings documentation you linked to included an example of that approach. I scanned that page a few times and came away with the impression that multi-line strings required using "here strings". cheers! – orion elenzil Dec 07 '21 at 21:02
  • 1
    Glad to hear it, @orionelenzil. I agree that the conceptual [about_Quoting_Rules](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules) help topic could use improvement, and I encourage you to open an issue in the PowerShell documentation GitHub repo at https://github.com/MicrosoftDocs/PowerShell-Docs/issues/new/choose – mklement0 Dec 07 '21 at 21:07
  • 1
    [added](https://github.com/MicrosoftDocs/PowerShell-Docs/issues/8393). amazing that such a large project has only 30 open issues ! – orion elenzil Dec 07 '21 at 21:18