Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign multiple lines string in Powershell Console

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 ?

like image 628
user310291 Avatar asked Aug 03 '15 17:08

user310291


People also ask

How do I create a multi line string in PowerShell?

A multiline string is a string whose value exceeds beyond one line. In that, the string should be enclosed within a here-string(@””@). Newline or carriage return value can also be used to create a multiline string. Multiline string can also be defined by using a line break within double-quotes.

How do I type multiple lines in PowerShell?

In PowerShell multiline command can be easily created using ` (backtick character) to split long or single line command into multiline statements. Backtick (`) character is used as escape character, it basically escape the newline character and results in line continuation.

How do I assign a string in PowerShell?

You can create a variable by simply assigning it a value. For example, the command $var4 = “variableexample” creates a variable named $var4 and assigns it a string value. The double quotes (” “) indicate that a string value is being assigned to the variable.

How do I make multiple lines on a string?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.


2 Answers

'@ 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 @"/"@

like image 62
user4003407 Avatar answered Sep 21 '22 18:09

user4003407


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.") 
like image 37
Carl Winbäck Avatar answered Sep 23 '22 18:09

Carl Winbäck