Question:
This is probably a super simple question, but I’m a bit frustrated now because I can’t find exactly what I’m looking for on the internet.
I am trying to convert a PowerShell DateTime to Epoch time using universal time.
I can do either, but can’t seem to find a working command to do both.
Here is what I know I can do:
1 2 3 |
PS C:\WINDOWS\system32> (Get-Date).ToUniversalTime() Wednesday, November 15, 2017 5:07:35 PM |
And:
1 2 3 |
PS C:\WINDOWS\system32> Get-Date -UFormat %s 1510747694.20287 |
But, how do I combine them so that I get seconds since epoch in UTC? Everything I try just gives me errors about unexpected token “UFormat” or String does not have method “ToUniversalTime.”
Thanks!
Answer:
If you look at Get-Help Get-Date
the first parameter is a -Date
option. We can use that to our advantage by wrapping our Get-Date
in another Get-Date
like so:
1 2 3 |
$epochseconds = Get-Date (Get-Date).ToUniversalTime() -UFormat %s return $epochseconds |