Reputation: 5
I am trying to convert a lot of dates from strings in some .log files to a different format. They currently show like:
Wednesday, June 29, 2016 12:15:27 PM.
I need to them to be in a 06-29-16 12:15:27 format.
I've got to do THOUSANDS of these conversions per file. I am trying to figure a way to automate the conversion via Powershell.
The sample below is just the first 5 lines.
So far, the closest I've gotten is ($dates contains the string(s):
$dates
Wednesday, June 29, 2016 12:04:51 PM
Wednesday, June 29, 2016 12:05:58 PM
Wednesday, June 29, 2016 12:07:00 PM
Wednesday, June 29, 2016 12:08:02 PM
Wednesday, June 29, 2016 12:09:03 PM
$dates | foreach ($_) {get-date -Format "MM-dd-yy hh:mm:ss"} returns:
07-19-16 08:11:16
07-19-16 08:11:16
07-19-16 08:11:16
07-19-16 08:11:16
07-19-16 08:11:16
07-19-16 08:11:16
07-19-16 08:11:16
That's just kicking out the current date/time. I need to convert the dates in the variable to their concomitant date/format.
I'm just really lost trying to convert the dates to another format. I know my syntax is wrong, somehow. But all the iterations I've tried don't work even as well as this!
I'm new to Powershell, but have been working my ass off to try to figure some of this out. This one is sticking me up.
Is there a way to pipe a variable with TONS of strings and get the date format changed?
Or should I be looking somewhere else for a simpler, more elegant solution?
Help?
Upvotes: 0
Views: 3416
Reputation: 1716
The problem is the way your foreach
is working:
$dates | foreach ($_) {get-date -Format "MM-dd-yy hh:mm:ss"}
Try this instead:
$dates | foreach {$_ | get-date -Format "MM-dd-yy hh:mm:ss"}
Or even simpler:
$dates | get-date -Format "MM-dd-yy hh:mm:ss"
Upvotes: 2