60

When I do enter this in powershell Console

$test=@'
Test
Test'@

And do enter several times, it keeps printing

>>

So I can never finish command.

What to do ?

CJBS
  • 15,147
  • 6
  • 86
  • 135
user310291
  • 36,946
  • 82
  • 271
  • 487

3 Answers3

100

'@ should be first thing in the line or it is considered to be just a part of the string.

$test=@'
Test
Test
'@

This approach also works with @"/"@

CJBS
  • 15,147
  • 6
  • 86
  • 135
user4003407
  • 21,204
  • 4
  • 50
  • 60
  • 3
    the '@ must start the line, in your code it is at the end of the line – It-Z Aug 03 '15 at 20:34
  • 6
    And the `"@` (or `'@`) needs to be at the very start of the final line, with nothing after it – CJBS Apr 25 '18 at 23:53
  • @CJBS *with nothing after it* Can you provide reference for this claim? – user4003407 Apr 26 '18 at 03:09
  • 2
    @PetSerAl I recorded my thoughts incorrectly. I meant nothing *before* the `'@` or `"@` on the final line. It's possible to have a command or comment *after* the "here string" terminator. – CJBS Apr 26 '18 at 17:08
17
$test=@'
Test
Test
'@

The important thing to note is that the delimiters include (invisible) carriage returns. There must be one at the end of the starting tag, and one before the closing tag.

metadaddy
  • 4,234
  • 1
  • 22
  • 46
Basu
  • 304
  • 2
  • 6
16

As per the section on maximum line length in The PowerShell Best Practices and Style Guide, I would suggest “splatting” the string, like this:

$myStr = ("The family of Dashwood had long been settled in Sussex. Their estate was " +
              "large, and their residence was at Norland Park, in the centre of their " +
              "property, where, for many generations, they had lived in so respectable " +
              "a manner as to engage the general good opinion of their surrounding " +
              "acquaintance.")
Carl Winbäck
  • 323
  • 4
  • 10
  • 6
    This is not an answer to the question. You are concatenating multiple strings to produce a string that does not contain any new line characters. What if you're trying to find and replace a multi-line string in another file? – Evil Pigeon Feb 26 '21 at 06:49