Powershell

How to Assign Multiple Lines String in PowerShell Console

The PowerShell console is an interpreter, which means it only stores and executes a code or data line by line. The PowerShell console executes and stores a single line string normally. However, multiple lines of strings can also be assigned and executed in PowerShell using a specific command.

This article will illustrate a procedure to rectify the mentioned query.

How to Assign Multiple Lines String in PowerShell Console?

These mentioned approaches in PowerShell can be utilized to assign a multiline string.

    • Here string (@” ”@)
    • Backtick character (`)

Method 1: Use the “Here String” Method in PowerShell to Assign Multi-Line String

The multiple lines string can be assigned by using the here-string (@” ”@) method. It is utilized to create multiple lines of string inside it. For instance, consider overviewing the below-mentioned example.

Example

Execute the following command:

> $String = @"
This is PowerShell
script code
demonstrated example.
"
@

 
In the above-stated code, we have used the (Here-String @””@) and added multi-lines of string inside it and assigned it to the “$String” variable:


Now, let’s verify whether the multiple lines of string were assigned or not by executing the given command:


The above output confirms that the multiple lines of strings have been saved successfully.

Method 2: Use the “Backtick Character” Method in PowerShell to Assign Multiple Lines String

Another method that can be used to assign multiple lines of string in PowerShell is the “Backtick `” operator of “n”. It is utilized to create new lines in PowerShell.

Example

This example will demonstrate to assign multiple lines of string in PowerShell by using the “Backtick `n operator”:

> $Str = "This is a `nPowerShell example `nof multiline strings"

 
In the above code:

    • We have added a text within inverted commas and used the “`n” operator between the text line to make it a multiple line string.
    • Finally, we assigned the text to a variable “$Str”:

 

Again, verify by accessing the value of the created variable:

> $Str

 

That was all about assigning multiple lines strings in PowerShell.

Conclusion

The multiple lines string in PowerShell can be assigned using several methods, including “Here-String @” ”@” and a “Backtick character n`”. The first method writes multiple lines of a string within these (@” ”@) symbols, while the second method breaks the lines to create multiple lines using the “`n” operator. This article has covered a detailed procedure to assign multiple lines of string in the PowerShell console.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.