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 :


Function Trace-Word
{
[Cmdletbinding()]
[Alias("Highlight")]
Param(
[Parameter(ValueFromPipeline=$true, Position=0)] [string[]] $content,
[Parameter(Position=1)]
[ValidateNotNull()]
[String[]] $words = $(throw "Provide word[s] to be highlighted!")
)
Begin
{
$Color = @{
0='Yellow'
1='Magenta'
2='Red'
3='Cyan'
4='Green'
5 ='Blue'
6 ='DarkGray'
7 ='Gray'
8 ='DarkYellow'
9 ='DarkMagenta'
10='DarkRed'
11='DarkCyan'
12='DarkGreen'
13='DarkBlue'
}
$ColorLookup =@{}
For($i=0;$i -lt $words.count ;$i++)
{
if($i -eq 13)
{
$j =0
}
else
{
$j = $i
}
$ColorLookup.Add($words[$i],$Color[$j])
$j++
}
}
Process
{
$content | ForEach-Object {
$TotalLength = 0
$_.split() | `
Where-Object {-not [string]::IsNullOrWhiteSpace($_)} | ` #Filter-out whiteSpaces
ForEach-Object{
if($TotalLength -lt ($Host.ui.RawUI.BufferSize.Width-10))
{
#"TotalLength : $TotalLength"
$Token = $_
$displayed= $False
Foreach($Word in $Words)
{
if($Token -like "*$Word*")
{
$Before, $after = $Token -Split "$Word"
#"[$Before][$Word][$After]{$Token}`n"
Write-Host $Before -NoNewline ;
Write-Host $Word -NoNewline -Fore Black -Back $ColorLookup[$Word];
Write-Host $after -NoNewline ;
$displayed = $true
#Start-Sleep -Seconds 1
#break
}
}
If(-not $displayed)
{
Write-Host "$Token " -NoNewline
}
else
{
Write-Host " " -NoNewline
}
$TotalLength = $TotalLength + $Token.Length + 1
}
else
{
Write-Host '' #New Line
$TotalLength = 0
}
#Start-Sleep -Seconds 0.5
}
Write-Host '' #New Line
}
}
end
{ }
}
#Trace-Word -content (Get-Content iis.log) -words "IIS", 's', "exe", "10", 'system'
view raw

Trace-Word.ps1

hosted with ❤ by GitHub

HOW TO RUN :

 

Highlight Words in PowerShell Console

LIMITATIONS: 

  1. 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 –Highlight Words in PowerShell Console

    THIS WON’T WORK –

    Highlight Words in PowerShell Console

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


Add-Content $profile -Value (iwr https://goo.gl/MX4F3v); . $Profile

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.

 

signature

Subscribe to our mailing list

* indicates required