Share via


Format CreationTime from Get-Item

Question

Friday, February 1, 2013 10:15 PM

I need to format the CreationTime from Get-Item as "yyyymmdd hh:mm:ss."

I can find tons of examples for Get-Date (i.e., -Format u) that I could substring(0,18), but nothing for the value returned from Get-Item.

How can I format a [datetime]$Variable to the string I need to output to file?

All replies (5)

Friday, February 1, 2013 10:39 PM ✅Answered

As follows:

get-item .\Backup | Select @{Name="LastWriteTime";Expression= {"{0:yyyy}-{0:mm}-{0:dd} {0:hh}:{0:mm}:{0:ss}" -f ([DateTime]$_.LastWriteTime)}}

Kind regards,

wizend

Month is expressed as M, minutes are m

Inspired by Heineken.


Friday, February 1, 2013 11:15 PM ✅Answered

Perfect. I knew there had to be a way, but Google was failing me! I did have to remove the dashes and change month from "mm" to "MM."


Friday, February 1, 2013 10:29 PM

How about this?

"{0:yyyyMMdd hh:mm:ss}" -f (Get-Date ((get-item 'c:\temp\smith_john.jpg').CreationTime))
20130126 07:34:51

Inspired by Heineken.


Friday, February 1, 2013 10:36 PM

As follows:

get-item .\Backup | Select @{Name="LastWriteTime";Expression= {"{0:yyyy}-{0:mm}-{0:dd} {0:hh}:{0:mm}:{0:ss}" -f ([DateTime]$_.LastWriteTime)}}

Kind regards,

wizend


Friday, February 1, 2013 11:22 PM

Thanks to you, too, or I would have missed the case requirement between month and minutes.