Excellent question! This is a very common point of confusion in PowerShell.
The short answer is: No, this will not work as you likely intend.
Here’s the detailed breakdown of why it won't work and how to fix it.
The Problem: [DateTime] vs. [TimeSpan]
-
(Get-Date).Subtract($StartTime): This part of your command calculates the difference between two points in time. The result is not another [DateTime] object (a point in time). Instead, it's a [System.TimeSpan] object, which represents a duration or interval of time.
-
"{0:HH:mm:ss.ff}": This is a custom format string specifically for [DateTime] objects.
HH: Hours of the day (00-23)
mm: Minutes of the hour (00-59)
ss: Seconds of the minute (00-59)
ff: Hundredths of a second
When PowerShell's format operator (-f) tries to apply a [DateTime] format string to a [TimeSpan] object, it doesn't know how to interpret it correctly. It will ignore your custom format and fall back to the default .ToString() method for the TimeSpan object.
What you would actually see:
Your code will output something like 00:00:12.3456789, which is the default format for a TimeSpan, completely ignoring your HH:mm:ss.ff specifier.
The Correct Solution
You need to use a format string designed for [TimeSpan] objects. The syntax is slightly different, and notably, you must "escape" the literal colon (:) and period (.) characters with a backslash (\) so they aren't interpreted as custom format specifiers themselves.
The correct format string is: "{0:hh\:mm\:ss\.ff}"
hh: Hours in the timespan (can be more than 23)
mm: Minutes
ss: Seconds
ff: Hundredths of a second
\:: A literal colon character.
\.: A literal period character.
Corrected Code:
Write-Host ("{0:hh\:mm\:ss\.ff}" -f (Get-Date).Subtract($StartTime))
Complete, Runnable Example
Here is a full example you can copy and paste to see the difference in action.
# 1. Set a starting time for our test
$StartTime = (Get-Date).AddSeconds(-15.75) # Start time is ~15.75 seconds ago
Start-Sleep -Milliseconds 100 # Wait a tiny bit more
# ---
# 2. Your original attempt (will not format as desired)
Write-Host "Original Attempt (Incorrect Output):"
$elapsedTime = (Get-Date).Subtract($StartTime)
Write-Host ("The elapsed time object type is: " + $elapsedTime.GetType().FullName)
Write-Host ("{0:HH:mm:ss.ff}" -f $elapsedTime)
Write-Host "" # Newline for clarity
# ---
# 3. The corrected version (works perfectly)
Write-Host "Corrected Version (Correct Output):"
Write-Host ("{0:hh\:mm\:ss\.ff}" -f $elapsedTime)
Expected Output:
Original Attempt (Incorrect Output):
The elapsed time object type is: System.TimeSpan
00:00:15.8624321
Corrected Version (Correct Output):
00:00:15.86
Bonus Tip: Measure-Command
For timing the execution of a script block, PowerShell has a built-in cmdlet that is often easier and more readable than managing $StartTime yourself.
$ExecutionTime = Measure-Command {
# Put the code you want to time here
Write-Host "Doing some work..."
Start-Sleep -Seconds 2
Write-Host "Work is done."
}
# The result has a TotalSeconds, TotalMilliseconds, etc. property
Write-Host ("Command took {0:N2} seconds to run." -f $ExecutionTime.TotalSeconds)
# You can also format the TimeSpan object directly
Write-Host ("Formatted time: {0:hh\:mm\:ss\.ff}" -f $ExecutionTime)