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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Prateek Singh
Related posts
5 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,132,483 People Reached
[…] on February 1, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
What’s up to all, it’s actually a good for me to visit this website,
it contains valuable Information.
[…] you find this script useful you may also like my previous blog post on highlighting keywords in PowerShell console, which work very well with this module, here is a screenshot of both working […]
great function, I finally got a grep in powershell, thx a lot Prateek !
The link does work, but you need the raw url. Otherwise everything will be put into $profile, which is a royal PITA: https://goo.gl/MX4F3v