Working with epoch time in PowerShell using UniversalTime

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:

And:

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:

Source:

Working with epoch time in PowerShell using UniversalTime by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply