7

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:

PS C:\WINDOWS\system32> (Get-Date).ToUniversalTime()
Wednesday, November 15, 2017 5:07:35 PM

And:

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!

Appleoddity
  • 647
  • 1
  • 6
  • 21

1 Answers1

11

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:

$epochseconds = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
return $epochseconds
Bryce McDonald
  • 1,760
  • 12
  • 22