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
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
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.
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)}
What would the PS command be, to add to or subtract from the timestamps, e.g. to compensate for wrong camera DST setting?
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!
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.
Nice!!! This tip worked just fine for me!
Thanks a lot, Martin!
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.
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?
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.
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.
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”)
Ups, sorry for that. Corrected and thanks!
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.