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?
Asked
Active
Viewed 3.3k times
21

Ansgar Wiechers
- 193,178
- 25
- 254
- 328

cloudnyn3
- 769
- 4
- 10
- 15
-
You can write multiple lines at once with `Write-Host`. – sodawillow Jun 27 '16 at 16:08
-
2Can 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? – Trey Nuckolls Jun 27 '16 at 16:37
3 Answers
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