Highlighting words in a Text content or Files in Powershell console


INTRODUCTION

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.

image

SCRIPT :

LIMITATIONS: 

  1. 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 –
    2
    THIS WON’T WORK –
    1

  2. 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.
  3. 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

fetimage

 

Hope you’ll find it useful in day-to-day troubleshooting using Powershell ! Please share your feedback and Thank you for reading 🙂

signature 

Advertisements

2 thoughts on “Highlighting words in a Text content or Files in Powershell console

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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