Unix Timestamp in PowerShell

PowerShell sits on .NET, so DateTimeOffset does the work. PowerShell 7 added -UnixTimeSeconds to Get-Date.

Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.

Current Unix timestamp

[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

# PowerShell 7+
Get-Date -UFormat %s

Timestamp to date

$ts = 1710050400
[DateTimeOffset]::FromUnixTimeSeconds($ts).UtcDateTime

# PowerShell 7+
Get-Date -UnixTimeSeconds $ts

# In a specific zone
$tz = [TimeZoneInfo]::FindSystemTimeZoneById('India Standard Time')
[TimeZoneInfo]::ConvertTime([DateTimeOffset]::FromUnixTimeSeconds($ts), $tz)

Date to timestamp

$d = [DateTimeOffset]::new(2024, 3, 10, 6, 0, 0, [TimeSpan]::Zero)
$d.ToUnixTimeSeconds()

([DateTimeOffset]::Parse('2024-03-10T06:00:00Z')).ToUnixTimeSeconds()

Converting a whole column

Import-Csv .\events.csv |
  Select-Object *, @{
    Name       = 'DateUtc'
    Expression = { [DateTimeOffset]::FromUnixTimeSeconds([long]$_.ts).UtcDateTime }
  } |
  Export-Csv .\events-readable.csv -NoTypeInformation

Pitfalls specific to PowerShell

  • Windows PowerShell 5.1 has no Get-Date -UnixTimeSeconds; use the [DateTimeOffset] form for scripts that must run on both.
  • Windows uses its own zone IDs ("India Standard Time") rather than IANA names on runtimes before .NET 6.

Rules that apply in every language

  1. Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
  2. Name the unit in the identifier. expires_at_ms rather than expires_at costs nothing and prevents the single most common timestamp bug.
  3. Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
  4. Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.

The same task in other languages

Frequently asked questions

How do I get the current Unix timestamp in PowerShell?

Use the snippet in the "Current Unix timestamp" section above. PowerShell works in .NET DateTimeOffset natively, so converting to another unit is a multiplication or an integer division away.

How do I convert a Unix timestamp to a date in PowerShell?

The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.

Does PowerShell handle time zones and daylight saving correctly?

Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways PowerShell makes that easy to get wrong.