Tuesday, June 28, 2011

Changing color of output in Powershell script

I found a neat little command today that will change the output color of the text:

Write-Host -foregroundcolor green "Operation succeeded!"

This will change the color of the text "Operation succeeded" to bright green on the screen. So that got me thinking on how I could use this and I came up with a couple solutions.

One situation where this would be handy is a UP/Down script. Say you wrote a script that would check to see if a server was up or down and wrote the output of UP in bright green letters on the screen. Also you could post DOWN in bright red letters. This is a visual cue that would help your sysadmins or helpdesk quickly identify if a server is up or down.

This also solves another issue that I am facing - hiding the output in a powershell script. I have been looking for ways to hide the output of my commands in powershell script. I found that you can pipe the output to a NULL statement but then you lose the output. I wanted to keep the output however just didn't want the user to see it on the screen. That is now possible with the "-foregroundcolor black" option. It will put the output from the command as black on the screen which will effectively hide it, but at the same time allowing me to highlight it and see what the output is if there is a problem.

No comments:

Post a Comment