How to edit timestamps with Windows PowerShell - gHacks Tech News

ADVERTISEMENT

How to edit timestamps with Windows PowerShell

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

Previous Post: «
Next Post: »

Comments

  1. 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.

  2. 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!

  3. 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.

  4. 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.

  5. 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?

  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. 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!

  8. 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.

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.