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.
Get the current Unix timestamp
# 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()
Convert a timestamp to a date
[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.
$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
Convert a date back to a timestamp
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.
# 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.
Kindmay beUtc,Local, orUnspecified; 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;
ConvertTimeBySystemTimeZoneIdhandles transitions correctly. - Rule: read with
[DateTimeOffset]::UtcNow, write with explicit offsets, convert zones only for display.
Native precision: milliseconds (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.
Common pitfalls
- [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")).
Try it live
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 protected] · Contact · Methodology
Sources & references
Unix time in every language
JavaScript · Python · PHP · SQL · Go · Java · C# · Rust · C++ · PowerShell
Prefer the point-and-click version? The epoch time converter on the home page handles seconds, milliseconds, and microseconds in any timezone — every snippet on this page produces the same value that tool shows.