Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

Solving typo problems with Fuzzy Search in PSWriteHTML

ConvertTo-HTML

One of the everyday use cases with PSWriteHTML is to create a simple view of PowerShell data in a table. While PowerShell comes with a built-in cmdlet ConvertTo-Html, it's basic in its functionality. It makes an HTML representation of PowerShell data, but it brings no CSS, JavaScript, or other functionality. While for some use cases, it's enough, the other times, you need to make an effort to make it usable.

ConvertTo-HTML vs Out-HtmlView vs New-HTMLTable

This is where the PSWriteHTML shines. With just a little effort, you get all sorts of ways to represent your PowerShell data with tables, charts, diagrams, gages, or even maps if there's a need for that. But one command I use in every single report New-HTMLTable and Out-HTMLView. The difference between the two is that New-HTMLTable acts a bit like ConvertTo-HTML, where it provides you a part of the HTML page where you can insert it into the New-HTML object. Out-HTMLView acts as an instant reporting tool, where it doesn't require any other things to do its magic. I already wrote a ton of blog posts on my website about PSWriteHTML, and if you want to find out more about those – please search my website, and you won't be disappointed. With the standard approach, you would use something like the code below, and then you would save it to an HTML file and get sort of basic-looking HTML.

Get-ADUser -Filter * | Select-object -First 1 | ConvertTo-Html

The three steps from above can be achieved with just one step using PSWriteHTML, and the result is much more user-friendly.

Get-ADUser -Filter * | Out-HTMLView

By default, you get the ability to export to Excel, CSV, and pdf. You also get a standard search and search builder, which gives you a custom way of searching thru the data. If you're interested in those options, you can find them in blog posts that I have already published. Today I wanted to talk about one new option, which is called Fuzzy Search.

In computer science, approximate string matching (often colloquially referred to as fuzzy string searching) is the technique of finding strings that match a pattern approximately (rather than exactly).

What does this mean in practice? If you're looking for Przemyslaw but type przemlaw in a regular search, you would get no results. The same goes for typing krgbt when you're looking for krbtgt. With just a simple switch you can now forget about typo problems!

As you can see, there are two new switches. The first one, FuzzySearch, enables fuzzy search, and the other one allows you to switch between fuzzy search and exact search within the interface. It's also possible to use FuzzySearchSmartToggle, which will enable FuzzySearch regardless of whether you use the FuzzySearch switch or not.

get-aduser -Filter * | Out-HtmlView -FuzzySearchSmartToggle
get-aduser -Filter * | Out-HtmlView -FuzzySearch -FuzzySearchSmartToggle

Both of those will give you the same result.

If you use the toggle in the right top corner, you get the ability to switch between standard and fuzzy options. This can be useful if a vague search will show a bit too much while you wanted an exact search. If you skip the FuzzySearchSmartToggle switch, you won't have an option to toggle between the two.

Using New-HTMLTable with FuzzySearch

Of course, Out-HTMLView is just a cmdlet to play quick reporting. Same fuzzy search is available within New-HTMLTable where you can apply it as you want

$Users = Get-ADUser -Filter * -Properties LastLogonDate, PasswordLastSet | Select-Object -First 6
New-HTML {
    New-HTMLTable -DataTable $Users -Title 'Table with Users' -HideFooter -PagingLength 5 -FuzzySearch {
        New-TableAlphabetSearch -ColumnName 'Name' -CaseSensitive -AddNumbers
    } -FuzzySearchSmartToggle

    New-HTMLTable -DataTable $Users -Title 'Table with Users' -HideFooter -PagingLength 5 {
        New-TableAlphabetSearch -ColumnName 'Name' -CaseSensitive -AddNumbers
    }
} -ShowHTML -FilePath "$PSScriptRoot\Example-FuzzySearch.html" -Online

As you can see above, I've only enabled FuzzySearch for one table (and with Alphabet Search as well). The other one has it disabled. I do hope this new feature will bring you as much joy as it did to me!

Get-Process | Out-htmlview -First 5 -FuzzySearchSmartToggle

Earlier resources for PSWriteHTML, Dashimo, Statusimo or Emailimo as PowerShell Modules

If you don't know PSWriteHTML, please read those articles below to understand how you can use its power to fulfill your goals. All the topics described above are just a small part of what PSWriteHTML can do.

To get it up and running, just install it from PowerShellGallery and you're good.

Install-Module PSWriteHTML -Force

While blog posts focus on PSWriteHTML, many other blog posts show different features that utilize PSWriteHTML to fulfill their goal.

Related Posts