16

April Fools PowerShell Prank - Write With All The Colors Of The Rainbow

 3 years ago
source link: https://thomasrayner.ca/april-fools-powershell-prank-write-with-all-the-colors-of-the-rainbow/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

April Fools PowerShell Prank - Write With All The Colors Of The Rainbow

Sometimes Write-Host gets a bad reputation. Lots of people will repeat inflammatory rhetoric that “Write-Host” kills puppies, and so on, but the only real problem with Write-Host is that people use it without knowing what it’s for. Write-Host is for writing to the console and only the console.

Other cmdlets like Write-Output are for writing to standard output which might be the console, or could be somewhere else down the pipeline. Write-Host’s output can’t be redirected to a log file, isn’t useful in unattended execution scenarios, and can’t be piped into another command. Lots of people who are new to PowerShell get into a habit of using Write-Host when they probably should have used Write-Output or something else instead. If you have someone you’re trying to train to stop using Write-Host when it’s not needed, consider this prank, just in time for April Fools Day.

There’s not much to it. Just add a couple of lines to their PowerShell profile.

$PSDefaultParameterValues.Add('write-host:foregroundcolor',{get-random $([system.enum]::getvalues([system.consolecolor]))})
$PSDefaultParameterValues.Add('write-host:backgroundcolor',{get-random $([system.enum]::getvalues([system.consolecolor]))})

This adds default values for the -BackgroundColor  and -ForegroundColor  parameters when Write-Host is called. If a script specifies a value for those parameters, those specific values will be used instead. If, instead, Write-Host is called without specifying values for the foreground and background color parameters, a random one will be used instead. It’ll look something like this.

2018-01-10-13_56_29-powershell.png
Written on March 28, 2018

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK