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.