PowerShell

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-WindowsTime

Convert from WindowsTime to PowerShell DateTime.

(optional) Return DateTime as Coordinated Universal Time

Convert-WindowsTime 132947402891099830
Convert-WindowsTime 132947402891099830 -UTC

Get-UnixTime

Convert PowerShell DateTime to Unix timestamp

Get-Date | Get-UnixTime
Get-Unixtime -datetime 'Sunday, 9 October 2022 2:47:48 PM'

Get-WindowsTime

Convert PowerShell DateTime to Windows timestamp

Get-Date | Get-WindowsTime
Get-WindowsTime -datetime 'Sunday, 9 October 2022 2:47:48 PM'

Summary

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

Darren Robinson

Bespoke learnings from a Microsoft Identity and Access Management Architect using lots of Microsoft Identity Manager, Azure Active Directory, PowerShell, SailPoint IdentityNow and Lithnet products and services.

Recent Posts

Visualising your IP Address using PowerShell and AI

A few weeks back the Microsoft AI Tour was in Sydney Australia. There was a…

3 weeks ago

Where the heck is the PowerShell Module loading from?

If you're anything like me you always have PowerShell open, and often both PowerShell and…

4 months ago

Express Verified ID Setup

Decentralised Identity is a technology I'm passionate about and have written many posts and tools…

5 months ago

Orchestrating 1Password with PowerShell

Over two years ago I authored a PowerShell Module that enabled the automation of 1Password.…

8 months ago

Entra ID Tenant ID & Custom Domains PowerShell Module

Buried in my PowerShell Snippets Vol 4 post from 2021 is the PowerShell script and…

8 months ago

Windows Subsystem for Linux instance has terminated

Short post on how to recovery from "The Windows Subsystem for Linux instance has terminated"…

9 months ago

This website uses cookies.