Hello guys, Today’s blog post is about a quick Function to Highlight a word or a group of words in a content and displaying it on the Powershell console in a visually pleasing way.
It can be used for Tracing instances of words from a log file, highlighting them and grouping same words with a randomly assigned color which will differ from word to word , which could be an advantage while reading / troubleshooting logs.
Like in the screenshot below I highlighted few words in the IIS logs on my machine.
- The function only works for single word as input, if you pass a group of words it won’t find anything. May be the feature I want to implement in next version of this script.
THIS WORKS –
THIS WON’T WORK –
- I’m wrapping the output of the function using Write-Host cmdlet which has really a bad name in the community [ not a good practice ], since it directs the output to the Host program instead of any output Streams. That means you can’t pipe the results or save it in a file. But that is OK, we just want to Trace/ Highlight the words so we can live with that even though it is not a good practice.
- I could have used $host.ui.RawUI.BackgroiundColor but I’m not very certain of its behavior on other Non-console based host like ISE.
HOW TO RUN :
Run the function like I did in the animation below and it will Highlight all words in their respective randomly assigned color codes
Hope you’ll find it useful in day-to-day troubleshooting using Powershell ! Please share your feedback and Thank you for reading 🙂