Find time in milliseconds using PowerShell?

You can get the full date with milliseconds with the following:

Get-Date -Format HH:mm:ss.fff

The question suggests finding a given datetime in milliseconds (Microsoft epoch time). This is easily solved with:

[Math]::Round((Get-Date).ToFileTime()/10000)

or

[Math]::Round((Get-Date).ToFileTimeUTC()/10000)

To convert this to Unix epoch time in seconds:

[Math]::Round((Get-Date).ToFileTime() / 10000000 - 11644473600)

Where 11644473600 is the number of elapsed seconds between the Microsoft epoch (January 1, 1601 A.D. (C.E.)) and the Unix epoch (January 1, 1970, 12AM UTC/GMT)

https://msdn.microsoft.com/en-us/library/system.datetime.tofiletime(v=vs.110).aspx


In PowerShell you can cast a time value to a timespan and call the TotalMilliseconds method:

([TimeSpan]"00:05:00").TotalMilliseconds # Returns 300000

([TimeSpan] (Get-Date).ToShortTimeString()).TotalMilliseconds # Returns total milliseconds from 00:00:00 till now