Reformat value from Milliseconds to Hours:Minutes;Seconds in Select-Object?


I am using the following Select-Object statement and I would like to re-format the $_.duration value from milliseconds to hours:minutes:seconds?

I have found the following gets me close; but it has the remaining milliseconds at the end of the string.

Select-Object @{N=‘Title’; E={$.title}}, @{N=“Duration H:M:S”; E={[timespan]::frommilliseconds($.duration).ToString()}},@{N=“Track ID”; E={$}}`

As an example, if $_.duration = “3215882” (so 53 Minutes, 35 seconds) it returns 00:53:35.8820000

How do I remove/mask the remaining milliseconds?

I would be grateful if someone could help me put this to bed; it feels so simple, but I am hitting a brick wall.

Thank you in advance.



3215882 | Select-Object @{N=“Duration H:M:S”; E={"{0:hh}:{0:mm}:{0:ss}" -f [timespan]::frommilliseconds($_)}}

Duration H:M:S



Thank you so much Aapeli.


For future folks looking at this post, you can also just use tostring() to format it.

$objects = @(
    title = "T1"
    duration = 3215882
    id = 123
    title = "T2"
    duration = 3245643
    id = 456

$objects |
Select-Object @{N='Title'; E={$_.title}}, @{N="Duration H:M:S"; E={[timespan]::frommilliseconds($_.duration).tostring("hh\:mm\:ss")}},@{N="Track ID"; E={$}}
Title Duration H:M:S Track ID
----- -------------- --------
T1    00:53:35       123
T2    00:54:05       456


Yes! That’s the way I would like to have done it too. I couldn’t get my head around the separator there. I was missing the \ escape.

Thanks for this!

Thank you too Curtis.