INTRODUCTION – Highlight Words in PowerShell Console

Hello guys, Today’s blog post is quick Function to highlight words/group of words from a content on Powershell console in a visually pleasing way.

It can be used for Tracing/highlighting words from a log file, and grouping same words with randomly assigned colors.

That means all word ‘Apple’ in the content will be assigned ‘Green’ color and all ‘Lemons’ will be ‘Yellow’ or something like that.

The function can enhance readability in PowerShell consoles, 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.

SCRIPT :

View the code on Gist.

HOW TO RUN :

LIMITATIONS:

The function accepts single words as array input, if you pass a group of words it won’t find anything.Maybe the feature I want to implement in next version of this script. THIS WORKS – THIS WON’T WORK – Wrapping the output using Write-Host cmdlet is not a very good practice and has a bad name in the community.Because it directs the output to the Host program instead of any output Streams.That means you can’t pipe the results or save it to 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.background color but I’m not very certain of its behavior on other Non-console based host like ISE.

To add this script to your Powershell profile run the below piece of code and you are good to go

View the code on Gist.

Let me know if find it useful in day-to-day work.

And If you like this script, I hope you’ll also love PowerShell Console Graphs take a look, cheers.

Follow @SinghPrateik



