Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to use here-strings in powershell 5.1 but not working

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading