Using PowerShell to change timestamp of files and folders

Last updated on 04th January 2017

Every file and folder in Windows have three timestamps to record the access, creation and modification times. This PowerShell tip shows you how to view and modify the creation time, modification time and access time files and folders in Windows .

You'll be very familiar with the dir command in MS-DOS which is used to list files and folders. The equivalent of dir in PowerShell is the Get-ChildItem cmdlet. Get-ChildItem cmdlet without any additional parameters shows you the files and folder objects in the currect location. This default listing contains basic file information such as name, size(Length), attributes(Mode) and modification time(LastWriteTime). The force parameter is used to show hidden and system files also.

PS C:\mytest> Get-ChildItem -force

    Directory: C:\mytest

Mode          LastWriteTime  Length Name
----          -------------  ------ ----
d----   04/01/2017    15:31         myfolder1
d----   04/01/2017    15:34         myfolder2
-a---   04/01/2017    15:32      48 myfile1.txt
-a---   04/01/2017    15:32      48 myfile2.txt
-a---   04/01/2017    15:32      48 myfile3.txt


Listing file and folder timestamps

The default ouput of Get-ChilItem does not contain the Creation time and Access time of files and folders, it only contains the Last write time. To view these timestamps also, you need to pipe the output of Get-ChildItem to Select-Object and explicitly get those timestamp properties. In the below example we also use the ft cmdlet to format the output in table format.

PS C:\mytest> Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft -autosize

Mode  Name        CreationTime        LastAccessTime      LastWriteTime
----  ----        ------------        --------------      -------------
d---- myfolder1   04/01/2017 15:31:46 04/01/2017 15:31:46 04/01/2017 15:31:46
d---- myfolder2   04/01/2017 15:31:51 04/01/2017 15:34:11 04/01/2017 15:34:11
-a--- myfile1.txt 04/01/2017 15:32:15 04/01/2017 15:32:15 04/01/2017 15:32:36
-a--- myfile2.txt 04/01/2017 15:32:54 04/01/2017 15:32:54 04/01/2017 15:32:36
-a--- myfile3.txt 04/01/2017 15:32:59 04/01/2017 15:32:59 04/01/2017 15:32:36

Note: Last Access Time updates are disabled by default in Windows 10 (and earlier versions since Windows Vista) to improve the performance of NTFS filesystem. You can read more about that in this article Enable Last Access timestamp for files and folder in Windows

Changing timestamp of a file or folder

To change the timestamp of a single file or folder you get that file or folder object with the Get-Item cmdlet and simply assign a time to the timestamp you want to change.

Example 1: Changing Creation time of a folder

PS C:\mytest> (Get-Item "C:\mytest\myfolder1").CreationTime=("08 March 2016 18:00:00")

Example 2: Changing modification time of a file

PS C:\mytest> (Get-Item "C:\mytest\myfile1.txt").LastWriteTime=("12 December 2016 14:00:00")

Example 3: Changing access time of a file

PS C:\mytest> (Get-Item "C:\mytest\myfile2.txt").LastAccessTime=("10 December 2015 14:00:00")

Now, lets verify the timestamps have changed.


PS C:\mytest> Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft -autosize

Mode  Name        CreationTime        LastAccessTime      LastWriteTime
----  ----        ------------        --------------      -------------
d---- myfolder1   08/03/2016 18:00:00 04/01/2017 15:31:46 04/01/2017 15:31:46
d---- myfolder2   04/01/2017 15:31:51 04/01/2017 15:34:11 04/01/2017 15:34:11
-a--- myfile1.txt 04/01/2017 15:32:15 04/01/2017 15:32:15 12/12/2016 14:00:00
-a--- myfile2.txt 04/01/2017 15:32:54 10/12/2015 14:00:00 04/01/2017 15:32:36
-a--- myfile3.txt 04/01/2017 15:32:59 04/01/2017 15:32:59 04/01/2017 15:32:36

Change timestamp of all files in current directory

This example shows you how to change timestamp of all files in the current directory and we want to do this only on files and not on any sub-directories. So first get all the files in the current location and use the Where-Object cmdlet to check the object attribute and filter out the sub-folders. Then we use a foreach loop to set the timestamp for each item. Here is the PowerShell script:


$files =  Get-ChildItem -force | Where-Object {! $_.PSIsContainer} 
foreach($object in $files)
{
     $object.CreationTime=("10 November 2016 12:00:00")
}


The above script will change the creation time of all files in the current directory. Let verify this now

PS C:\mytest> Get-ChildItem -force | Select-Object Mode, Name, CreationTime, LastAccessTime, LastWriteTime | ft -autosize

Mode  Name        CreationTime        LastAccessTime      LastWriteTime
----  ----        ------------        --------------      -------------
d---- myfolder1   08/03/2016 18:00:00 04/01/2017 15:31:46 04/01/2017 15:31:46
d---- myfolder2   04/01/2017 15:31:51 04/01/2017 15:34:11 04/01/2017 15:34:11
-a--- myfile1.txt 10/11/2016 12:00:00 04/01/2017 15:32:15 12/12/2016 14:00:00
-a--- myfile2.txt 10/11/2016 12:00:00 10/12/2015 14:00:00 04/01/2017 15:32:36
-a--- myfile3.txt 10/11/2016 12:00:00 04/01/2017 15:32:59 04/01/2017 15:32:36

If you want to do this recursively to all files in the sub-directories also then all you need to do is use the -recurse parameter with Get-ChildItem cmdlet.


Post a comment

Comments

Minerva | September 10, 2023 9:25 PM |

I'm not even close to being technically competent, but I found this to be quite doable for me. Something that keeps happening to my files (Windows 10 OS) is that it changes a bunch of the file dates (not just the system ones) to match the last upgrade. It's really annoying.

arjuna | August 20, 2020 7:52 PM |

Get-ChildItem -File -Recurse |%{$_.CreationTime = $_.LastAccessTime = $_.LastWriteTime = Get-Date}

Renegade | November 8, 2021 12:47 AM |

Works like a charm. Thank you so much.

oh noes | October 3, 2019 3:16 PM |

$files = Get-ChildItem -force -recurse | Where-Object {! $_.PSIsContainer} foreach ($object in $files) { $object.CreationTime=("1 October 2019 16:20:00") } At line:1 char:93 + ... curse | Where-Object {! $_.PSIsContainer} foreach ($object in $files) ... + ~~ Unexpected token 'in' in expression or statement. At line:1 char:92 + ... -re

for each | October 4, 2019 12:17 PM |

This line foreach ($object in $files) must be in a new line.. its not a continuation of Where-Object {! $_.PSIsContainer}

Dave Wanner | April 9, 2019 5:00 AM |

This is awesome, thanks!! I accidentally modified a document on my hard drive and saved it without renaming it first. Saving the one that I had sent in an e-mail a month earlier resulted in it getting today's timestamp. With these PowerShell commands, I was easily able to re-timestamp it to within one minute of the original timestamp, without downloading some sketchy attribute changing utility!

Plesky | February 13, 2019 8:52 AM |

Hello, can you sent me How change CreationTime and LastAccessTime whitch is store in CSV? Thank you

erick | July 2, 2018 3:16 AM |

I am beng hacked and notice that everythjng is on West Coast time zone and Im on eastern. is this a tactic used to hack into ones system?!!any way that we can keep it from happening

sam | December 11, 2017 4:47 PM |

THANKS