21

I've done a bit of Googling and can't seem to find an effective method of displaying an entire block of text to the console. I would rather not use the Write-Host command on every line if I need to display a block of code. I'm trying to make an interactive script that's somewhat aesthetic. Is there an example that someone could give me?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
cloudnyn3
  • 769
  • 4
  • 10
  • 15

3 Answers3

40

PowerShell supports multiline strings, either as here-strings:

Write-Host @"
Some text you
want to span
multiple lines.
"@

or regular strings:

Write-Host "Some text you
want to span
multiple lines."
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thank you for the clear answer. I don't know why I couldn't find something online that was clean and clear. The other examples I'd found were 4 - 5 lines of code just to accomplish this. I knew it couldn't be that hard! – cloudnyn3 Jun 27 '16 at 18:19
9

In addition to Ansgar's examples, Write-Host accepts an array too.

'one','two','three' | Write-Host

So whether your multi-line string is a single string, or an array of lines, it will still work as expected with a single Write-Host call:

Get-Content mycode.txt | Write-Host
Get-Content mycode.txt -Raw | Write-Host
briantist
  • 45,546
  • 6
  • 82
  • 127
0
echo '''multiline
text
here''' > filename.txt

Works as well

Fran
  • 3,693
  • 4
  • 19
  • 19