Life in a colour… :)

Yesterday I was in a strange mood. I had a lot of work, some repetitive yet not easy to automate tasks. You know, one of those boring and yet busy days. It usually helps me to do something silly on those days. Create a script or a function that does not do anything useful, but shows how flexible powershell is. So… I was thinking about the way to make PowerShell output colourful. At first – randomly colourful:

PowerShell, using GeSHi
  1. function outcolour {
  2.         if ($Input) {
  3.                 foreach ($obj in ($Input | Out-String -Stream)) {
  4.                         foreach ($char in $obj.ToCharArray()) {
  5.                                 Write-Host -ForegroundColor $(GetRandom -InputObject $( (0..15) | ? { $_ -ne $Host.UI.RawUI.BackgroundColor.value__ } ) )   $char -NoNewline
  6.                         }
  7.                         "`r"
  8.                 }
  9.         }
  10. }

The result is…. chaotic, to say the least. 😀 And it’s really SLOW, so there is probably a lot of space for performance improvement here. 😉


Next step – make it more clear and easier to use. First – what was easier to achieve – make each row have random colour:

PowerShell, using GeSHi
  1. function outcolourByLine {
  2.         if ($Input) {
  3.                 foreach ($obj in ($Input | Out-String -Stream)) {
  4.                                 Write-Host -ForegroundColor $(GetRandom -InputObject $( (0..15) | ? { $_ -ne $Host.UI.RawUI.BackgroundColor.value__ } ) )   $obj
  5.                 }
  6.         }
  7. }

Results more readable for sure:

Out-ColourByLine But I guess the best would be to have columns single-coloured. But to get it I would probably need to dig more into formatting system, and it makes it not so silly and so entertaining any more. 😉 So I leave it to the experts. 😉 BTW: I wish I could dot-source stuff in PowerShellPlus library, I have that functions there and I have to edit/ paste it into console to get it to work. In that area ISE is more… intuitive. Select, hit F8 and you are done. 😉 I hope that this is only my ignorance and this tool actually supports such actions… :>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s