I’m getting red squiggles when I try to use here-strings in PS 5.1. What am I missing?
function Main {
$csv = @"
test1,test2
"@
echo $csv
}
Main
Error:
White space is not allowed before the string terminator.
>Solution :
Note:
-
The following applies up to at least PowerShell 7.3.2
-
Improvements to the here-string syntax have been green-lit – both to support indentation and a single-line variant – but not yet implemented – see GitHub issue #2337.
PowerShell’s here-strings have strict syntax requirements with respect to the closing delimiter ('@ or "@):
- It must be at the very start of the line – not even whitespace is allowed to precede it.
This makes it tricky to use with indented code, because the indentation cannot be maintained, as you’ve experienced:
@'
hi there
'@ # OK: closing delimiter is at the *very start* of the line.
@'
hi there
'@ # !! BROKEN -> error "White space is not allowed before the string terminator."
Additionally, note that here-string content isn’t indentation-aware either:
- That is, the resulting verbatim value in the first example (the working one) is
hi there, i.e. including the leading whitespace.