How do I output ASCII Art to console?
Solution 1
Just use a newline delimited string, here-string or Get-Content -Raw
if you have a source file. Both will give you a single multiline string without fuss. One thing the here-string is good for is not having to worry about the quotes you use (which could be a distinct possibility with ASCII art). An example using a here-string would be the following:
$text = @"
"ROFL:ROFL:ROFL:ROFL"
_^___
L __/ [] \
LOL===__ \
L \________]
I I
--------/
"@
Or if you choose to go the source file approach:
$text = Get-Content -Raw $path
Or if you only have PowerShell 2.0 $text = Get-Content $path | Out-String
Either way you can follow up with Write-Host
or whatever you want after that.
Getting funky with colours would require some special logic that, as far as I know, does not currently exist. Making a colourized output randomizer is simple enough. To get a basic idea I present Get-Funky
function Get-Funky{
param([string]$Text)
# Use a random colour for each character
$Text.ToCharArray() | ForEach-Object{
switch -Regex ($_){
# Ignore new line characters
"`r"{
break
}
# Start a new line
"`n"{
Write-Host " ";break
}
# Use random colours for displaying this non-space character
"[^ ]"{
# Splat the colours to write-host
$writeHostOptions = @{
ForegroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
# BackgroundColor = ([system.enum]::GetValues([system.consolecolor])) | get-random
NoNewLine = $true
}
Write-Host $_ @writeHostOptions
break
}
" "{Write-Host " " -NoNewline}
}
}
}
That will take a newline delimited string and use random host colours for displaying the output. We use splatting with $writeHostOptions
so you could easily control the colours. You could even have parameters that force one of the colours or disabled colourizing of one etc. Here is some sample output:
$art = " .:::. .:::.`n:::::::.:::::::`n:::::::::::::::
':::::::::::::'`n ':::::::::'`n ':::::'`n ':'"
Get-Funky $art
Heart ascii art found at asciiworld.com
Solution 2
It's a piece of my PowerShell $PROFILE
:
# Personalize the console
$Host.UI.RawUI.WindowTitle = "Windows Powershell " + $Host.Version;
# Draw welcome screen
Write-Host -ForegroundColor DarkYellow " _oo0oo_"
Write-Host -ForegroundColor DarkYellow " o8888888o"
Write-Host -ForegroundColor DarkYellow " 88`" . `"88"
Write-Host -ForegroundColor DarkYellow " (| -_- |)"
Write-Host -ForegroundColor DarkYellow " 0\ = /0"
Write-Host -ForegroundColor DarkYellow " ___/`----'\___"
Write-Host -ForegroundColor DarkYellow " .' \\| |// '."
Write-Host -ForegroundColor DarkYellow " / \\||| : |||// \"
Write-Host -ForegroundColor DarkYellow " / _||||| -:- |||||- \"
Write-Host -ForegroundColor DarkYellow " | | \\\ - /// | |"
Write-Host -ForegroundColor DarkYellow " | \_| ''\---/'' |_/ |"
Write-Host -ForegroundColor DarkYellow " \ .-\__ '-' ___/-. /"
Write-Host -ForegroundColor DarkYellow " ___'. .' /--.--\ `. .'___"
Write-Host -ForegroundColor DarkYellow " .`"`" '< `.___\_<|>_/___.' >' `"`"."
Write-Host -ForegroundColor DarkYellow " | | : `- \`.;`\ _ /`;.`/ - ` : | |"
Write-Host -ForegroundColor DarkYellow " \ \ `_. \_ __\ /__ _/ .-` / /"
Write-Host -ForegroundColor DarkYellow " =====`-.____`.___ \_____/___.-`___.-'====="
Write-Host -ForegroundColor DarkYellow " `=---='"
# Create frequent commands
New-Alias -Name vsc -Value "D:\Program Files\VSCode\Code.exe";
$HOSTS = "$env:SystemRoot\system32\drivers\etc\hosts";
$Desktop = "$env:USERPROFILE\Desktop"
$Documents = "$env:USERPROFILE\Documents"
$TimestampServer = "http://timestamp.digicert.com"
Set-Location D:\Scripts;
Just a reference.
Solution 3
PowerShell can do multi-line strings (with all kinds of strings; this isn't limited to here-strings), so the following should work, actually:
Write-Host 'first line
second line
third line'
Of course, if you're not in a function and don't care about any return values, you can just omit Write-Host
completely. If you're reading from an external file, then in the same vein, Get-Content
would just work:
Get-Content ascii.txt
if you really need Write-Host, then just use
Get-Content | % { Write-Host $_ }
Solution 4
Here-strings are also perfectly good arguments to Write-Host
:
$multiplelines = @"
Multi-lines?!
Well ... Why
not?
"@
Write-Host $multiplelines
Solution 5
Below is an example that does multi-line using Write-Host
Write-Host " *** ** ** ** ** * ********
>> **** ******* ********* *
>> ** ***** ******** *
>> * *** ****** *
>> * **** *
>> **
>> *
>> "
UPDATED: Removed the backslashes as they are not needed in PowerShell
jward01
I am learning code and addicted! I love the logic, problem solving and seeing how/why the code works. Programming Legend
Updated on June 27, 2022Comments
-
jward01 almost 2 years
I would like to spice up my scripts with some ASCII art to be displayed on completion of a process.
I have 2 thoughts on how to output ASCII art to console. Hopefully someone who knows more than I do can direct us to what the command is and what method is 'Better'.
Output a multi-line
Write-Host
? I tried to do this earlier and it didnt work. It threw a bunch of errors. Maybe I wasnt doing the 'multi-line' version (if a multi-line) version exists.Save the ASCII art to a .txt file, then in my script somehow grab and read the contents of that .txt file and then write the contents to the console in the specified area.
Which way is better? How can I implement this?
-
Rich over 8 yearsUhm, what purpose would the backslashes serve there?
-
vmachan over 8 yearsTo continue the line on the next line.. I guess you can also do without it
-
Ansgar Wiechers over 8 yearsBackslashes aren't escape characters in PowerShell strings, and escaping isn't even required here.
-
Matt almost 6 yearsI would totally convert that to a single multi line string. That way you would only have to change the colour once!
-
Chenry Lee almost 6 yearsWrite the pattern to
@'blabla'@
is much easier to read, I just copied it from Internet. :)