Unix timestamps in PowerShell
Get the current epoch time and convert between timestamps and dates in PowerShell. Every snippet below is copy-ready.
// note
DateTimeOffset avoids the local-time surprises of Get-Date.
now.ps1 · current timestamp
$now = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$nowMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
timestamp → date
$ts = 1700000000
$dt = [DateTimeOffset]::FromUnixTimeSeconds($ts)
$dt.UtcDateTime.ToString("o")
date → timestamp
$ts = [DateTimeOffset]::Parse("2023-11-14T22:13:20Z")
.ToUnixTimeSeconds()
// current epoch
— seconds since 1970-01-01T00:00:00Z.
Watch out for these
- Seconds vs milliseconds. An epoch value 13 digits long is milliseconds; 10 digits is seconds. Mixing them gives dates in 1970 or 55828.
- Local vs UTC. Format functions that default to the machine's timezone will silently differ between servers.
- Leap seconds. Unix time ignores them — the count is a simple 86,400 seconds per day.
Reference: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date