If you’ve ever needed to convert a time value — like 01:30:00 or a DateTime — into total seconds in PowerShell, you know it’s one of those tasks that sounds simple but can trip you up if you don’t know the right approach.
I’ve been through that confusion myself. In this tutorial, I’m going to walk you through five practical methods for converting time to seconds in PowerShell. I’ll cover time strings, TimeSpan objects, DateTime subtraction, Unix epoch conversion, and measuring elapsed script time. By the end, you’ll know exactly which method fits your situation.
Let’s get into it.
Why You’d Need This
Before jumping into the methods, let me quickly explain why converting time to seconds is so common in PowerShell scripting:
- Comparing durations — You can’t do math on time strings directly. Seconds make comparison easy.
- Unix/Epoch timestamps — Many APIs and log systems work in epoch seconds (seconds since January 1, 1970).
- Script performance monitoring — When you want to see how long a script or a block of code took to run.
- Scheduling and delays —
Start-Sleepaccepts seconds, so you need to convert time values into that format. - Reporting — When building dashboards or logs, standardizing to seconds makes things consistent.
Alright, let’s look at the methods one by one.
Method 1: Use New-TimeSpan and TotalSeconds
This is the most straightforward method, and it’s the one I reach for first. The New-TimeSpan cmdlet creates a TimeSpan object — which is PowerShell’s way of representing a duration. Once you have a TimeSpan, you can grab the .TotalSeconds property directly.
Example: Convert hours, minutes, seconds to total seconds
$timeSpan = New-TimeSpan -Hours 1 -Minutes 30 -Seconds 45
$timeSpan.TotalSeconds
# Output: 5445
It’s that simple. You pass in the hours, minutes, and seconds, and .TotalSeconds gives you the full value as a decimal number.
You can see the exact output in the screenshot below:

What if you want a whole number (no decimals)?
[int]$timeSpan.TotalSeconds
# Output: 5445
Casting to [int] rounds it down to the nearest whole number. If you want to round properly instead of truncate, use [math]::Round():
[math]::Round($timeSpan.TotalSeconds)
Example: Just minutes to seconds
$timeSpan = New-TimeSpan -Minutes 45
$timeSpan.TotalSeconds
# Output: 2700
Clean, readable, and no complicated math needed.
Check out How to Concatenate String and DateTime in PowerShell
Method 2: Convert a Time String Like “hh:mm:ss” to Seconds
This is a very common scenario. You have a time string — maybe it came from a CSV, a log file, or user input — and you want to turn it into seconds. Here’s how.
Using [TimeSpan]::Parse()
$timeString = "01:30:45"
$timeSpan = [TimeSpan]::Parse($timeString)
$timeSpan.TotalSeconds
# Output: 5445
[TimeSpan]::Parse() is a .NET method that understands the standard hh:mm:ss format. It returns a TimeSpan object, and from there you already know what to do — just grab .TotalSeconds.
Here is the exact output in the screenshot below:

What about mm:ss format (no hours)?
$timeString = "30:15"
$timeSpan = [TimeSpan]::Parse("00:$timeString")
$timeSpan.TotalSeconds
# Output: 1815
If your string doesn’t include hours, just prepend "00:" to make it valid for parsing. Quick and reliable.
What if the string has milliseconds?
$timeString = "01:30:45.500"
$timeSpan = [TimeSpan]::Parse($timeString)
$timeSpan.TotalSeconds
# Output: 5445.5
[TimeSpan]::Parse() handles milliseconds too, which is great if you’re working with precise timestamps.
Read Convert a String to an Array of Characters in PowerShell
Method 3: Subtract Two DateTime Objects
Sometimes you don’t have a fixed time value — you have a start time and an end time, and you want to know the difference in seconds. Subtracting two DateTime objects in PowerShell gives you a TimeSpan automatically.
Example: Difference between two specific times
$startTime = Get-Date "2026-05-10 08:00:00"
$endTime = Get-Date "2026-05-10 09:45:30"
$elapsed = $endTime - $startTime
$elapsed.TotalSeconds
# Output: 6330
No extra cmdlets needed. When you subtract one DateTime from another, PowerShell returns a TimeSpan, and .TotalSeconds gives you the duration in seconds.
You can see the exact output in the screenshot below:

Real-world use case: How long did your script take to run?
$start = Get-Date
# --- your script logic here ---
Start-Sleep -Seconds 3
# ---------------------------------
$end = Get-Date
$duration = $end - $start
Write-Host "Script took $([math]::Round($duration.TotalSeconds, 2)) seconds"
# Output: Script took 3.01 seconds
This is genuinely useful for performance testing. I use this pattern all the time when I want a quick gut check on how long a block of code runs.
Read Convert Bytes to Base64 String in PowerShell
Method 4: Convert DateTime to Unix Epoch Seconds
Unix time (also called epoch time) is the number of seconds since January 1, 1970, 00:00:00 UTC. It’s widely used in APIs, logging systems, databases, and Linux-based environments. Here are two clean ways to do this in PowerShell.
Method 4a: Using New-TimeSpan with the Unix Epoch
$epoch = Get-Date "01/01/1970 00:00:00"
$targetDate = Get-Date "2026-05-10 08:00:00"
$unixSeconds = (New-TimeSpan -Start $epoch -End $targetDate).TotalSeconds
[int]$unixSeconds
# Output: 1746864000 (approximate)
Method 4b: Using DateTimeOffset (Cleaner and More Reliable)
[System.DateTimeOffset]::new((Get-Date)).ToUnixTimeSeconds()
# Output: 1746864000 (current time in epoch seconds)
This is my preferred approach for Unix time. The DateTimeOffset class is designed specifically to handle time zones correctly, which matters a lot when you’re dealing with UTC vs. local time mismatches.
Converting epoch seconds BACK to a readable date
$unixTime = 1746864000
[DateTimeOffset]::FromUnixTimeSeconds($unixTime).LocalDateTime
# Output: Saturday, May 10, 2026 8:00:00 AM (your local time)
This round-trip capability is super handy when debugging timestamps from APIs or log files.
Important note on time zones: If you’re computing epoch time manually using New-TimeSpan, your result could be off by hours depending on your system’s UTC offset. Always work with UTC when dealing with epoch timestamps. Use (Get-Date).ToUniversalTime() to convert first:
$epoch = [DateTime]"1970-01-01T00:00:00Z"
$utcNow = (Get-Date).ToUniversalTime()
$unixSeconds = ($utcNow - $epoch).TotalSeconds
[int]$unixSeconds
Read Convert Base64 String to Byte Array in PowerShell
Method 5: Use Measure-Command to Get Execution Time in Seconds
If you specifically want to measure how long a command or script block takes to execute (and get the result in seconds), Measure-Command is the cleanest tool for the job.
Basic example
$result = Measure-Command {
Start-Sleep -Seconds 2
}
$result.TotalSeconds
# Output: 2.0123456...Measure-Command wraps your code in a script block {}, runs it, and returns a TimeSpan. The .TotalSeconds property gives you the exact duration.
Rounding to 2 decimal places
$result = Measure-Command {
# Your code here
Start-Sleep -Milliseconds 1500
}
[math]::Round($result.TotalSeconds, 2)
# Output: 1.5Practical use: Benchmark two approaches
$method1 = Measure-Command { 1..10000 | ForEach-Object { $_ * 2 } }
$method2 = Measure-Command { for ($i = 0; $i -lt 10000; $i++) { $i * 2 } }
Write-Host "Method 1: $($method1.TotalSeconds) sec"
Write-Host "Method 2: $($method2.TotalSeconds) sec"This is perfect for comparing performance between two different approaches — something I find really useful when optimizing scripts that process large datasets.
Check out Convert Base64 String to Text in PowerShell
Quick Reference: Which Method Should You Use?
Here’s a simple cheat sheet to help you pick the right approach:
| Scenario | Best Method |
|---|---|
| Convert hours/minutes/seconds to total seconds | New-TimeSpan + .TotalSeconds |
Parse a time string like "01:30:00" | [TimeSpan]::Parse() |
| Difference between two DateTime values | Subtract DateTimes, use .TotalSeconds |
| Convert date to Unix/Epoch seconds | DateTimeOffset.ToUnixTimeSeconds() |
| Measure how long a script block runs | Measure-Command |
Bonus: [TimeSpan]::FromSeconds() — Going the Other Way
Just in case you need to go in reverse — you have a number like 3661 and want to turn it back into a proper time format — here’s how:
$seconds = 3661
$ts = [TimeSpan]::FromSeconds($seconds)
Write-Host "$($ts.Hours)h $($ts.Minutes)m $($ts.Seconds)s"
# Output: 1h 1m 1s
Or format it as hh:mm:ss:
$ts.ToString("hh\:mm\:ss")
# Output: 01:01:01The backslashes before the colons are required to escape the colon characters inside the format string — something that trips people up the first time.
Don’t Make These Mistakes
A few things I’ve seen go wrong when working with time conversions in PowerShell:
- Using
.Secondsinstead of.TotalSeconds—.Secondsonly gives you the seconds component (0–59), not the total. If you have a 90-second span,.Secondsgives30, not90. Always use.TotalSecondswhen you want the full value. - Ignoring time zones with epoch conversions — This leads to results that are off by hours. Use UTC consistently.
- Parsing
mm:ssstrings without prepending hours —[TimeSpan]::Parse("30:15")will throw an error. Add"00:"in front. - Forgetting to cast to
[int]when needed —.TotalSecondsreturns aDouble. If your downstream code expects an integer, cast it explicitly.
Conclusion
In this tutorial, I explained how to convert time to seconds in PowerShell using various methods. Whether you’re parsing time strings, working with Unix timestamps, measuring script performance, or just converting hours and minutes into a flat number — there’s a clean, built-in way to do it.
The TimeSpan object is really the heart of all of this. Once you’re comfortable working with it, most time-related tasks in PowerShell become much simpler.
You may also like:
- Convert String to Object in PowerShell
- Convert String to Date in PowerShell
- Convert String to Base64 in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.