Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Archives
April 3, 2026

More PowerShell Water Tools

In this issue:

  • Convert-Water
    • Remainder Math
    • Custom Formatting
  • Extending the Class
  • Summary

Last time we looked at ways to use PowerShell as a presentation tool. I want to continue exploring that idea and also return to the tools I built to track my water consumption. Who knew drinking water could be this much fun!

A few weeks ago I shared the PowerShell tools I use to document how much water I drink a day. I know that staying well hydrated is a good thing, but I was never one to drink a lot of water. But now that I am recording it, I find myself drinking more. PowerShell made it easy and fun.

I'm storing my data in a JSON file and have commands to read the data.

PS C:\> Get-Water -Days 2

Date                 Amount Comment
----                 ------ -------
4/1/2026 7:45:00 AM       8
4/1/2026 8:44:28 AM       8
4/1/2026 10:59:19 AM     16
4/1/2026 3:51:06 PM      16
4/1/2026 6:44:21 PM      16
4/2/2026 8:12:53 AM       8
4/2/2026 12:58:46 PM     16
4/2/2026 5:49:17 PM      16
4/2/2026 6:35:00 PM       8
4/2/2026 9:00:51 PM       8

And because I want to know what this means in aggregate, I have a measure function.

PS C:\> Measure-Water -Days 7

Date                  Count Average TotalOz
----                  ----- ------- -------
3/27/2026 12:00:00 AM   4   14           56
3/28/2026 12:00:00 AM   5   12.8         64
3/29/2026 12:00:00 AM   5   12.8         64
3/30/2026 12:00:00 AM   5   12.8         64
3/31/2026 12:00:00 AM   5   11.2         56
4/1/2026 12:00:00 AM    5   12.8         64
4/2/2026 12:00:00 AM    5   11.2         56

Convert-Water

As I look at this report, obviously 64 ounces per day is the target. But how much is that? It would might be useful to get a report that shows the amounts in quarts and gallons. Especially when measuring consumption over a longer time period.

I can start with this breakdown.

  • 16 oz = 1 pint
  • 32 oz = 1 qt
  • 64 oz = 1/2 gal
  • 128 oz = 1 gal

I want to convert these values into a descriptive expression. I want something that will convert 56 ounces into 1 quart 1 pint 8 ounces.

Remainder Math

Naturally, this will require a little math. I'll start at the bottom of the scale. I want to take value between 16 and 32 ounces. Anything less than 16 will be straight ounces. But I want to take value like 24 and show it as 1 pint 8 ounces

$x = 24

I can divide this by 16 to get the total number of pints.

PS C:\> $x/16
1.5

Clearly that is 1 1/2 pints. For my string output, I need to get the integer part of the value, or 1. I could use a regular expression, but I'll use the Truncate() method of the [Math] class. I don't want to round the value because that might go up. The Truncate() method is how you can round "down".

$pints =  [Math]::Truncate($x/16)

Now I need to get the remainder, or the .5 value. This is a situation where you can use the modulo (%) operator. This will return the remainder value.

$oz = $x%16

I can put all of this together to get my description.

PS C:\> "{0} pint {1} ounces" -f $pints,$oz

1 pint 8 ounces

I can put this in a script block to test. This is using the PowerShell 7 ternary operator to determine if there are any ounces to display.

$pt = {
    param ($amount)
        [int]$pints = [Math]::Truncate(($amount / 16))
        [int]$oz = $amount % 16
        ($oz -gt 0) ? "$pints pint $oz ounces" : "$pints pint"
}

This will work for any amount, but the value will be in pints and ounces.

PS C:\> &$pt 16
1 pint
PS C:\> &$pt 24
1 pint 8 ounces
PS C:\> &$pt 28
1 pint 12 ounces
PS C:\> &$pt 44
2 pint 12 ounces

I can continue this process moving up in volume such as quarts

PS C:\> $x = 44
PS C:\> $x/32
1.375

This equates 44 ounces to 1 quart and .375 quarts which equates to 12 ounces.

PS C:\> $x = 44
PS C:\> $x/32
1.375

I can write a similar script block for quarts.

$qt = {
    param ($amount)
        [int]$qts = [Math]::Truncate(($amount / 32))
        [int]$pints = $amount % 32
        #convert pint value to pints and ounces
        $out = ($pints -gt 0) ? "$qts quart $(&$pt $pints)" : "$qts quart"
        $out
}

If there is a pint value, I can convert that to pints and ounces.

$qt = {
    param ($amount)
        [int]$qts = [Math]::Truncate(($amount / 32))
        [int]$pints = $amount % 32
        #convert pint value to pints and ounces
        $out = ($pints -gt 0) ? "$qts quart $(&$pt $pints)" : "$qts quart"
        $out
}
Want to read the full issue?
GitHub
Bluesky
LinkedIn
Mastodon
jdhitsolutions.github.io
Powered by Buttondown, the easiest way to start and grow your newsletter.