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?
3 Answers
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."
-
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! Jun 27, 2016 at 18:19
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
Write-Host
.write-host
on multiple times to show up the way you want?