22

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?

2
  • You can write multiple lines at once with Write-Host.
    – sodawillow
    Jun 27, 2016 at 16:08
  • 2
    Can you provide an example of a block of text you are having to use write-host on multiple times to show up the way you want? Jun 27, 2016 at 16:37

3 Answers 3

41

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."
1
  • 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, 2016 at 18:19
10

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
0
echo '''multiline
text
here''' > filename.txt

Works as well

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.