Search code examples
windowspowershellfile-properties

How to convert File CreationTime into String in Powershell


All I found online were ways to write the current date into a string using get-date.toString()

What I want to do is read the CreationDate and Time from a File and convert that into a String for a directory name.

It would suffice if i could save the different parts of CreationTime into separate Variables.

E.g.: $Year, $Month, $Day, $Hour etc.

How would I go about doing that?


Solution

  • See https://technet.microsoft.com/en-us/library/ee692801.aspx .

    i.e.

    (gi C:\temp\trim.csv).CreationTime.ToString('ddd MM dd yyyy')
    

    will give you a string "Wed 05 27 2015"

    UPD: or if you want to use string as a directory name just delete the spaces.

    (gi C:\temp\trim.csv).CreationTime.ToString('dddMMddyyyy')
    

    and get "Wed05272015"