Convert to and from Windows and Unix timestamps with PowerShell

In this post I detail a module I’ve just published that is another of those I’ve done this before, but how do I find it again moments. Converting from Windows and Unix timestamps with PowerShell. A number of the PowerShell Modules I’ve published do contain the Unix timestamp conversion, but I couldn’t find Windows timestamp conversion as a function that I know I’ve previously written.

Therefore, in this post I detail the functions in the ConvertTime PowerShell module I’ve just published which allows you to convert to and from a PowerShell DateTime object and Unix & Windows timestamps. But what are Windows and Unix timestamps?

Windows Timestamps

A Windows timestamp is an 18-digit integer often referred to as an Active Directory timestamp, and more historically Windows32 FILETIME or SYSTEMTIME. In PowerShell it is also referenced as FileTime. In Active Directory it is the value used for attributes such as pwdLastSet, accountExpires, LastLogon, LastLogonTimestamp, and LastPwdSet. The 18-digit timestamp is the number of 100-nanosecond intervals (one billionth of a second) since Jan 1, 1601 UTC (Windows epoch).

example 132947402891099830

Unix Timestamps

The Unix timestamp is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT – Unix epoch), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Often these values are seen in APIs for lastUpdatedTime or similar.

example 1592001868

The Module

The module has been tested in Windows PowerShell (5.1) and PowerShell (7.x) on Windows.

The module contains four cmdlets (two with optional switches to return conversion result as UTC time) are:

  • Convert-UnixTime
  • Convert-WindowsTime
  • Get-UnixTime
  • Get-WindowsTime

Installing the module

Install direct from the PowerShell Gallery (Powershell 5.x and above)

install-module -name ConvertTime

Convert-UnixTime

Convert from Unix timestamp to a PowerShell DateTime Object relative to local time based of system time zone.

(optional) Return DateTime as Coordinated Universal Time

Convert-UnixTime 1592001868
Convert-UnixTime 1592001868 -UTC
Convert Unix timestamps with PowerShell to DateTime PSObject

Convert-WindowsTime

Convert from WindowsTime to PowerShell DateTime.

(optional) Return DateTime as Coordinated Universal Time

Convert-WindowsTime 132947402891099830
Convert-WindowsTime 132947402891099830 -UTC
Convert Windows timestamps with PowerShell to DateTime PSObject

Get-UnixTime

Convert PowerShell DateTime to Unix timestamp

Get-Date | Get-UnixTime
Get-Unixtime -datetime 'Sunday, 9 October 2022 2:47:48 PM'
Convert DateTime PowerShell Object to Unix timestamp

Get-WindowsTime

Convert PowerShell DateTime to Windows timestamp

Get-Date | Get-WindowsTime
Get-WindowsTime -datetime 'Sunday, 9 October 2022 2:47:48 PM'
Convert DateTime PowerShell Object to Windows timestamp

Summary

A versatile PowerShell Module for generating or converting Unix and Windows timestamps to and from PowerShell DateTime objects.