Build a hexadecimal clock in PowerShell – Part 2

Doctor Scripto

Summary: Manipulate string data from Get-Date in PowerShell.

Honorary Scripting Guy, Sean Kearney, is here today to have a little more fun with our silly project to build a hexadecimal clock in the PowerShell console.

Well, after all, who said scripting wasn’t allowed to be fun? That’s usually how I learn, by playing about!

Yesterday, we defined a cool array of Here-Strings that contain all the characters that we’d like to display in our clock. Today, we’re going to access the properties of the current time and see how we could put that to use.

The very first thing that we’ll need to do is pull up the date and time and store that away in an object.


Good. We’ve got the current date and time. If you run Get-Member against this, you’ll see some very useful properties, namely, hour, minute, and second.

Screenshot of results of Get-Member which returns hour, minute, and second among other properties.

We can access any one of these objects by just connecting the dots. No, literally!

$Now.Hour $Now.Minute $Now.Second

This is perfect! We can easily access the data we need. Now, all we need to do is get each one as a hexadecimal number.

To do this, we just need to use this [Convert] accelerator. Converting a single number to hexadecimal in PowerShell is easy. Here, we convert 42 to Hex:


If you really want to be nerdy, you could try Octal by changing the 16 to an 8 for…“Base 8”.


So, for each number, we’ll need to convert the information to a hex digit like so:

$HexHour=[Convert]::tostring($Now.Hour,16) $HexMinute=[Convert]::tostring($Now.Minute,16) $HexSecond=[Convert]::tostring($Now.Second,16)

But, we have one extra problem. When I’m done, I want to have two digits for every column. I want to have this evenly spaced and have a consistent number of characters.

A built-in method that we can use for strings is padleft(), which allows us to specify how many characters the output should have at minimum and what and where to “pad the blanks with”.

The padleft method requires two properties: the number of characters and just what you’re going to pad it with.

We will make sure that each position has two (2) characters and will always have at least a single ‘0’ preceding it. It (padleft) will look like this:


So, when we pull up the time and convert hours, minutes, and seconds to hex, it will look like this:

$HexHour=[Convert]::tostring($Now.Hour,16).padleft(2,’0’) $HexMinute=[Convert]::tostring($Now.Minute,16).padleft(2,’0’) $HexSecond=[Convert]::tostring($Now.Second,16).padleft(2,’0’)

For the final time piece, we’ll wrap it all together as a single string and make this a basic function:

Function Get-HexTime()


$Now=Get-Date $hour=[Convert]::tostring(($now.Hour),16).padleft(2,'0') $Minute=[Convert]::tostring(($now.Minute),16).padleft(2,'0') $Second=[Convert]::tostring(($now.Second),16).padleft(2,'0')

Return ("$Hour-$Minute-$Second")


Getting the time in hex is very easy now. We just execute the Get-HexTime function.

Screenshot of the Get-HexTime function.

Swing on over to the Scripting Guys tomorrow when we start to play with how to parse this data and access the array!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then always remember that with Great PowerShell comes Great Responsibility.

Sean Kearney Honorary Scripting Guy Cloud and Datacenter Management MVP


Discussion is closed.

Feedback usabilla icon