PowerShell

Solving typo problems with Fuzzy Search in PSWriteHTML

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

This post was last modified on %s = human-readable time difference 23:42

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

1 month ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

2 months ago

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

7 months ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

11 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

1 year ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

1 year ago