How do I output ASCII Art to console?
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
It's a piece of my PowerShell $PROFILE
:
# Personalize the console$Host.UI.RawUI.WindowTitle = "Windows Powershell " + $Host.Version;# Draw welcome screenWrite-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 commandsNew-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.
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 linesecond linethird 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 $_ }