I recently had a peer reach out and ask if there was a simple way to convert Epoch to Windows time. Being the quick thinker and lover of PowerShell I was I quickly responded with a resounding – YES!
I told him, “Send me an Epoch (Unix) time and I’ll convert it in a jiffy.” and so he did, and thus the best converter in the world was born!!!
Fun fact before we get started:
On Friday, February 13, 2009 at 23:31:30, the Unix time was 1234567890.
So here’s The Skinny:
$UnixEpochTime = 1484784000000 $Date = Get-Date "1/1/1970" $UnixTime = $UnixEpochTime / 1000 $date.AddSeconds($UnixTime).ToLocalTime()
In the first line, you can see we hardcoded the Epoch to test a known fixed time
Next, we initialize a date in the format he wants it in.
Then, we divide Epoch time by 1000.
Finally, we take the calculated Epoch and add that time to our local time to get………
Wednesday, January 18, 2017 7:00:00 PM
I’d recommend putting a read-host in there so it can take any input or turn it into a function so you can just make a call to something like a -Time parameter and get the output. You can find the full code here on my GitHub as well.