Unix timestamps in PowerShell
PowerShell has no single epoch cmdlet that works in every version, but the .NET DateTimeOffset type does, and it behaves the same in Windows PowerShell 5.1 and PowerShell 7.
Current timestamp
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds() # → e.g. 1763164800
[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() # → e.g. 1763164800123These call .NET directly, so there is no rounding and no dependence on the local time zone.
Timestamp to date
Every example uses 1700000000, which is 14 November 2023 at 22:13:20 UTC, so you can compare the output across languages or check it in the timestamp converter.
$d = [DateTimeOffset]::FromUnixTimeSeconds(1700000000)
$d.ToString('yyyy-MM-ddTHH:mm:ssZ') # → 2023-11-14T22:13:20Z
$d.UtcDateTime.ToString('u') # → 2023-11-14 22:13:20Z
[TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($d, 'Eastern Standard Time').ToString('yyyy-MM-dd HH:mm:ss zzz') # → 2023-11-14 17:13:20 -05:00
[DateTimeOffset]::FromUnixTimeMilliseconds(1700000000123).ToString('HH:mm:ss.fff') # → 22:13:20.123$d.LocalDateTime gives the same instant as a [datetime] in the computer's own zone, and $d.UtcDateTime in UTC. Zone IDs such as Eastern Standard Time are the Windows names; list them with Get-TimeZone -ListAvailable.
Date to timestamp
([DateTimeOffset]'2023-11-14T22:13:20Z').ToUnixTimeSeconds() # → 1700000000
[DateTimeOffset]::new(2023, 11, 14, 22, 13, 20, [TimeSpan]::Zero).ToUnixTimeSeconds() # → 1700000000
([DateTimeOffset]'2023-11-14T17:13:20-05:00').ToUnixTimeSeconds() # → 1700000000Casting a string to [DateTimeOffset] keeps the offset written in it. A string with no offset is read as local time.
PowerShell 7 shortcuts and the old way
Get-Date -UFormat %s # current epoch seconds
Get-Date -UnixTimeSeconds 1700000000 # epoch to local [datetime] (PowerShell 7.1 and later)[int64]((Get-Date '2023-11-14T22:13:20Z').ToUniversalTime() - [datetime]'1970-01-01').TotalSeconds # → 1700000000The subtraction works on any version, including machines with a .NET Framework older than 4.6. In Windows PowerShell 5.1, Get-Date -UFormat %s is not safe to use: it returns a value with a decimal fraction and does not convert local time to UTC, so the result is wrong by your UTC offset.
Pitfalls
Get-Datereturns local time withKindset to Local. Call.ToUniversalTime()before subtracting the epoch, as above; skipping it is the classic off-by-your-offset bug.[datetime]'2023-11-14T22:13:20Z'converts to local time as it parses. The instant is right, but the hours you see are not UTC.- Culture. Casting a string to
[datetime]uses the invariant culture (month first), whileGet-Date '03/04/2024'uses the current culture. ISO 8601 strings avoid the question.
Questions
How do I get the Unix timestamp in PowerShell?
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds() works in Windows PowerShell 5.1 and PowerShell 7. In PowerShell 7 you can also use Get-Date -UFormat %s.
How do I convert a Unix timestamp to a date in PowerShell?
[DateTimeOffset]::FromUnixTimeSeconds(1700000000).LocalDateTime for local time or .UtcDateTime for UTC. PowerShell 7.1 and later also has Get-Date -UnixTimeSeconds 1700000000.