Unix Timestamp in PowerShell

PowerShell uses [DateTimeOffset]::UtcNow for Unix timestamps. The ToUnixTimeSeconds() and ToUnixTimeMilliseconds() methods are available in .NET 4.6+ and all PowerShell 5.1+ environments.

Code Examples

Current timestamp (seconds)

[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()

The cleanest approach. DateTimeOffset is timezone-aware and returns a [long] (Int64). Works in PowerShell 5.1 and PowerShell 7+.

Current timestamp (milliseconds)

[DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()

Returns milliseconds since epoch as a [long]. Useful when calling JavaScript or Java APIs that expect 13-digit timestamps.

Alternative — epoch math for older environments

$epoch = [DateTime]::new(1970,1,1,0,0,0,[DateTimeKind]::Utc)
(Get-Date).ToUniversalTime() - $epoch | Select-Object -ExpandProperty TotalSeconds

Manual epoch calculation — works on PowerShell versions without .NET 4.6. The result is a [double], so cast to [int] or [long] if you need an integer.

Convert timestamp to human-readable date

[DateTimeOffset]::FromUnixTimeSeconds(1708560000).UtcDateTime

Converts a Unix timestamp (seconds) back to a [DateTime] in UTC. Append .ToLocalTime() to display in the local timezone.

Store timestamp in a variable and format

$ts = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
Write-Output "Timestamp: $ts"
[DateTimeOffset]::FromUnixTimeSeconds($ts).ToString("yyyy-MM-ddTHH:mm:ssZ")

Capture the timestamp in a variable, log it, and format the equivalent date as ISO 8601. Useful in deployment scripts and log analysis.

Note

PowerShell runs on .NET, so [DateTimeOffset]::UtcNow is the recommended approach — consistent with C# best practices. Note that Get-Date -UFormat %s is a Unix-only strftime format and is not reliably supported on Windows. Always use [DateTimeOffset] rather than [DateTime] for Unix timestamps to avoid timezone ambiguity.

Main Epoch Converter

Use the homepage when you need epoch to date, date to epoch, or a full epoch converter

This PowerShell guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.

Related Tools

More free tools for developers debugging timestamps, schedules, and text changes.