Monthly Archives: August 2021

PowerShell Humanizer – manipulate strings your way

Simplicity boils down to two steps: Identify the essential. Eliminate the rest.

Leo Babauta

In the world of Web API’s and string exchange between CLI and REST there is a need to make those responses more human readable. String can represents dates, timespans, numerals, etc. and are computer generated. Of course you can write script (module in PowerShell) to modify / transform text to be more readable, but that can be a tedious and repetitive task.

After searching through PowerShell gallery for a while, I found the perfect module which has it all – PowerShell Humanizer.

What is Humanizer?

Humanizer is powerful .NET library for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities.

PowerShell humanizer is a wrapper around .NET Humanizer version, which gives you :

Check this video from Nick Chapsas going into more details how .NET version works:

How to work like a pro with Humanizer

How do I install it?

PowerShell humanizer is built as a module. You can easily install it via PowerShell gallery:

PowerShell module
Install-Module -Name PowerShellHumanizer

After installation you will need to import the module to be able to use it.

Import-Module PowerShellHumanizer

If you want to load it on each terminal session, add the upper line to default PowerShell profile.

import module in PowerShell default profile

Few examples and usages

If you check commands from the module, you’ll see quite some useful ConvertTo methods:

PowerShell humanizer methods

Let’s use Humanizer with some data from publicly available API. Let’s use random user generator.

Random user API call from PowerShell
$users = (Invoke-WebRequest "https://randomuser.me/api" -UseBasicParsing).Content | ConvertFrom-Json -Verbose

Let’s store the variable in $firstUser.

$firstUser=$users.results[0]

Let’s use the humanizer to give us information about how much this user is old (ToWords() function):

Write-Host $firstUser.name.first $firstUser.name.last "is" $firstUser.dob.age.ToWords() "(" $firstUser.dob.age ") old"
Getting info about how much person is old

We can traverse through the list and get back more readable information (from date of birth – get-date with function Humanize()):

$firstUser | select @{ Label="Was born"; Expression={(get-date $_.dob.date).Humanize()}}, @{Label="Full name";Expression={$_.name.first +" " + $_.name.last}}
Using custom expression to get data

In some cases you don’t want to show the full data (either to long ort sensitive). With humanizer this is an easy task to accomplish:

$firstUser.login.uuid.Truncate(15)
truncate options

There are a lot of possibilities out there, I touched only tip of the ice berg.

Check this additional examples from the module author to learn more.

Conclusion

PowerShell has powerful built-in tools to manipulate objects and strings. In some cases you need advanced functionalities and human readable form. Humanizer solves that problem and it is easy to install and powerful to use.

I will for sure add it to my default PowerShell profile for daily usages.