Categories

Changing folder creation date

A question on the forum asked about setting creation date on folders after they have been copied to match the source folder.

I created a source folder with three folders and modified the creation dates

Set-ItemProperty -Path c:\testsource\folder1 -Name CreationTime -Value ((get-date).adddays(-90))
Set-ItemProperty -Path c:\testsource\folder2 -Name CreationTime -Value ((get-date).adddays(-127))
Set-ItemProperty -Path c:\testsource\folder3 -Name CreationTime -Value ((get-date).adddays(-192))

 

I then created a file

dir c:\scripts |out-file -FilePath test.txt

and copied it into each folder

 

The copy of the folders and their contents is achieved like this

Copy-Item -Path c:\testsource\* -Destination c:\testtarget -Force –Recurse

 

Our source looks like this

PS> Get-ChildItem -Path c:\testsource | where {$_.PSIsContainer} | ft  Fullname, CreationTime -a

FullName                             CreationTime
--------                                    ------------
C:\testsource\Folder1   13/11/2011 11:26:35
C:\testsource\Folder2   07/10/2011 11:27:12
C:\testsource\Folder3   03/08/2011 11:27:27

 

and the target looks like this

PS> Get-ChildItem -Path c:\testtarget | where {$_.PSIsContainer} | ft  Fullname, CreationTime -a

FullName                           CreationTime
--------                                  ------------
C:\testtarget\Folder1   11/02/2012 11:34:25
C:\testtarget\Folder2   11/02/2012 11:34:25
C:\testtarget\Folder3   11/02/2012 11:34:25

 

So our job is to change the creationtime property on the target folders

 

Get-ChildItem -Path c:\testsource -Recurse |             
where {$_.PSIsContainer} |            
foreach {            
 $newfolder = $_.Fullname -replace "Testsource", "Testtarget"            
            
 Set-ItemProperty -Path $newfolder -Name CreationTime -Value $($_.CreationTime)            
}


 



Just loop through the source folders – change the path to match the target and set the CreationTime



 



We can then test the results



PS> Get-ChildItem -Path c:\testtarget | where {$_.PSIsContainer} | ft  Fullname, CreationTime -a



FullName                           CreationTime
--------                                  ------------
C:\testtarget\Folder1   13/11/2011 11:26:35
C:\testtarget\Folder2   07/10/2011 11:27:12
C:\testtarget\Folder3   03/08/2011 11:27:27



 



Job done

Leave a Reply