How to edit timestamps with Windows PowerShell

Martin Brinkmann
Oct 9, 2017
Updated • Oct 9, 2017
Windows, Windows tips
|
12

The following tutorial demonstrates how you can edit file timestamps -- create, last access and last write -- using Windows PowerShell.

PowerShell is part of any modern version of Microsoft's Windows operating system. Microsoft shifted to PowerShell away from the Command Prompt on Windows 10, but did not remove the command prompt doing so.

Windows 10 users and administrators have access to both, but the development focus lies clearly on PowerShell.

Each file on Windows, and other operating systems as well, has several timestamps associated with it. The file system keeps track of the files creation time, last access time, and last write time.

How to edit timestamps with Windows PowerShell

powershell change timestamp

First thing you need to do is to launch a PowerShell window.  Tap on the Windows-key, type PowerShell and hit the Enter-key afterwards. Note that this launches the prompt under the current user account. If you require an elevated prompt, for instance to edit file timestamps of folders that the user has limited access to, hold down Shift-key and Ctrl-key additionally.

The three commands that you require are the following ones:

  • $(Get-Item FILENAME.EXT).creationtime=$(DATE)
  • $(Get-Item FILENAME.EXT).lastaccesstime=$(DATE)
  • $(Get-Item FILENAME.EXT).lastwritetime=$(DATE)

The three commands change the creation, last access and last write timestamps of the file when you run them.

Note: Last Access Time is not enabled by default on all supported versions of Windows because of performance concerns.

To give you some examples:

  • $(Get-Item test.txt).creationtime=$(Get-Date)
  • $(Get-Item test.txt).lastaccesstime=$(Get-Date "12/24/2011 07:15 am")

The first command sets the creation timestamp of the file text.txt to the current date and time. The second command changes the last access time and date to December 24th, 2011 at 7:15 am.

Note that the command requires that the file is in the current directory of the PowerShell prompt.

Helpful commands

Once thing that may be useful is to list the file timestamps of the current folder before and after you run the PowerShell command. This makes it easier to find files that still require changing, and check whether the changes have been applied correctly.

Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft

powershell list timestamps

The command lists all files and folders of the current path, and displays the creation time, last access time and last write time of each item in a table.

  • -force in this context includes hidden and system files in the output.
  • ft is short for format table.

If you just need the create timestamp, run Get-ChildItem -force instead.

The following script runs the operation on all files.

$modifyfiles = Get-ChildItem -force | Where-Object {! $_.PSIsContainer}
foreach($object in $modifyfiles)
{
$object.CreationTime=("11/11/2011 12:00:00")

$object.LastAccessTime=("11/11/2011 12:00:00")

$object.LastWritetime=("11/11/2011 12:00:00")

}

Just copy and paste it, and change it according to your requirements.

Tip: If you prefer a graphical user interface when it comes to changing timestamps, check out the free software program Attribute Changer instead.

Summary
How to edit timestamps with Windows PowerShell
Article Name
How to edit timestamps with Windows PowerShell
Description
The following tutorial highlights how you can edit file timestamps -- create, last access and last write -- using Windows PowerShell.
Author
Publisher
Ghacks Technology News
Logo
Advertisement

Tutorials & Tips


Previous Post: «
Next Post: «

Comments

  1. adiva said on November 9, 2021 at 1:27 pm
    Reply

    Great! here you are a short version of the script:
    $modifyfiles= Get-ChildItem -force | Where-Object {! $_.PSIsContainer}
    $data= “01/01/2021 12:00:00”
    foreach($object in $modifyfiles){ $object.CreationTime=( $data); $object.LastAccessTime=( $data); $object.LastWritetime=( $data)}

  2. Polyvios Simopoulos said on December 7, 2020 at 5:41 pm
    Reply

    What would the PS command be, to add to or subtract from the timestamps, e.g. to compensate for wrong camera DST setting?

  3. Oren_Yul said on June 18, 2020 at 7:51 pm
    Reply

    Great! thanks a lot.
    Worked almost without any glitch… One issue – I use a Hebrew system so the date/time did not work.
    I simply first run “$(Get-Date)” to see the required format. Than I just used the date portion [ I do not care about the time]… and it worked like a charm.

    Thank you!

  4. Daniel said on May 19, 2020 at 12:04 pm
    Reply

    Very impressive! Thanks for sharing.

    How would you go about adopting a time stamp taken from the file name? E.g. file called 20180304_165225.jpg -> new time stamp 2018/03/04 4:52:25 pm.

    Use case: I got lots of old photo files, which, after moving them around between clouds and devices, unfortunately lost their original time stamp. The jpg’s however got their original time stamp as their file name which I was hoping to use as the new time stamp.

  5. NESTOR NOGUEIRA DE ALBUQUERQUE said on April 12, 2020 at 5:21 am
    Reply

    Nice!!! This tip worked just fine for me!
    Thanks a lot, Martin!

  6. Jake J said on February 11, 2020 at 12:17 pm
    Reply

    I have a problem that may be impossible, or just impossible for me to figure out because I’m an idiot.
    In powershell I need to change all the files with the directory to a new creation date, and I do so with:

    gci -recu -inc “*.*” | % { $_.creationtime = Get-Date }

    Now, what I’ve been trying to do and failing is automate this recursively so that it changes each file (they’re alphabetical or 1,2,3,4,5,etc) one after another but with different times. i.e. First file is 01 second, second is 02 second, and so on up and up through all the files. I have given up and can’t get it to work.

  7. Mark said on February 6, 2020 at 11:24 pm
    Reply

    i tried to change the creation date to “03/12/1979 7:15 am” and it wouldnt let me. (the creation date was cleared on the file) It WAS able to do 03/12/1980 though. What’s up with that?

  8. Keijo said on November 14, 2019 at 7:58 am
    Reply

    there is 4 dates per files. created, modified and access which you teached. but the explorer shows also “Date” which is none of these and cant be modified with powershell.

  9. MK said on October 16, 2017 at 9:33 am
    Reply

    And “12/24/2011 07:15” won’t work for non-english versions (e.g. german) or might confuse month and date (e.g. canadian?).
    “2011-12-24 07:15 am” (or 07:15 pm) should work in all cases.

  10. jupe said on October 9, 2017 at 11:10 am
    Reply

    note in the example given the missing end quote if it doesn’t work for you

    “12/24/2011 07:15 am)

    should be

    “12/24/2011 07:15 am”)

    1. Martin Brinkmann said on October 9, 2017 at 11:19 am
      Reply

      Ups, sorry for that. Corrected and thanks!

  11. RossN said on October 9, 2017 at 10:56 am
    Reply

    Looks useful. I may be able to simplify a complex batch script I use to reset the date and time of an email archive that gets changed when emClient is closed.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.