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.
Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.
← Open the converter