Real Epoch Converter logo realepochconverter
programming guide

PowerShell Unix Timestamp: Get-Date, DateTimeOffset & Ticks

PowerShell offers two very different "now" objects: [datetime] (naive, machine-local) and [datetimeoffset] (aware, carries an offset). Every Unix-time helper lives on the offset type — here is the complete, copy-paste pattern for scripts and one-liners.

[DateTimeOffset]::UtcNow and its accessors

PowerShell
# Seconds and milliseconds, explicitly UTC
[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()       # 1767225600
[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()  # 1767225600123

# One-liner via Get-Date (returns a [datetimeoffset] in PS 7)
(Get-Date).ToUnixTimeSeconds()

FromUnixTimeSeconds() and zone conversion

[DateTimeOffset]::FromUnixTimeSeconds(ts) returns a UTC DateTimeOffset. Display it with .ToString("u") (UTC) or convert to a named zone with [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId. Avoid Get-Date -UFormat %s for precision work — it has historical quirks and returns a string.

PowerShell
$dto = [DateTimeOffset]::FromUnixTimeSeconds(1767225600)
$dto.ToString("yyyy-MM-dd HH:mm:ss zzz")            # 2026-01-01 00:00:00 +00:00

# Named zone (DST-aware on Windows via TimeZoneInfo)
$ist = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($dto, "India Standard Time")
$ist.ToString("yyyy-MM-dd HH:mm:ss")                # 2026-01-01 05:30:00

Back to a timestamp: pin the offset explicitly

Build a [DateTimeOffset] with an explicit offset — never a bare [datetime], whose Kind is Unspecified and gets treated as local. Get-Date -Date '2026-01-01' -AsUTC is the clean PS 7 way to pin input to UTC.

PowerShell
# Explicit UTC offset — unambiguous
$dto = [DateTimeOffset]::new(2026, 1, 1, 0, 0, 0, [TimeSpan]::Zero)
$dto.ToUnixTimeSeconds()                             # 1767225600

# PS 7: Get-Date with -AsUTC
(Get-Date -Date '2026-01-01 00:00:00' -AsUTC).ToUnixTimeSeconds()

# ⚠ naive [datetime] is machine-local:
([datetime]'2026-01-01 00:00:00').ToUniversalTime()

Naive vs aware: [datetime] vs [datetimeoffset]

  • [datetime] — naive. Kind may be Utc, Local, or Unspecified; the last is assumed local by most conversions.
  • [datetimeoffset] — aware: wall-clock plus offset. All ToUnixTime* methods live here.
  • TimeZoneInfo — the DST-aware path for named zones; ConvertTimeBySystemTimeZoneId handles transitions correctly.
  • Rule: read with [DateTimeOffset]::UtcNow, write with explicit offsets, convert zones only for display.

Native precision in PowerShell: 100ns ticks underneath

.NET's [datetime]/[datetimeoffset] carry 100-nanosecond ticks, so UtcNow is millisecond-resolution on Windows. For the raw .NET value (100ns intervals since 0001-01-01), $dto.Ticks exposes it — that's the value the .NET ticks converter on this site decodes.

PowerShell pitfalls: Kind=Unspecified and Get-Date -UFormat

  • [datetime] Kind=Unspecified — .ToUniversalTime() silently applies the machine offset.
  • Get-Date -UFormat %s — string output, historically buggy across versions; use ToUnixTimeSeconds().
  • Windows zone names — "India Standard Time" not "Asia/Kolkata"; on PowerShell 7 (.NET with ICU) IANA names also work.
  • CSV/JSON round-trips — serializing naive datetimes loses the zone; emit ISO 8601 with offset (.ToString("o")).

Check the PowerShell output in the live converter

Check any value in the epoch converter, or copy the current timestamp from the PowerShell snippet tab on the homepage.

First published · Last reviewed · Maintained and developed by the Real Epoch Converter team · Email · Contact · Methodology

the same task in every language

JavaScript · Python · PHP · SQL · Go · Java · C# · Rust · C++ · PowerShell

the browser-based epoch time converter rounds out the CLI toolkit when PowerShell is not in front of you.

Copied