Modify file write time in Powershell
The goal here is to fix file dates that were messed up after copying a huge amount of files from one server to another. We will look up the source file in $lookupPath
then set the same modified date/time on the new file in $replacedPath
.
$lookupPath = 'H:\Shares\Data\Documentation\Data'
$replacedPath = 'C:\\Users\\USERNAME\\Data\\Documentation\\Data'
ForEach ($file in $(Get-ChildItem -Recurse)) {
$shortLocal = $file.FullName -Replace "$replacedPath",""
Write-Host "shortLocal is $shortLocal" -ForegroundColor Green
Write-Host "Time on shortLocal is" $(Get-Item $shortLocal).LastWriteTime -ForegroundColor Green
$longRemote = $lookupPath + $shortLocal
Write-Host "longRemote is $longRemote" -ForegroundColor Cyan
Write-Host "Time on longRemote is" $(Get-Item $longRemote).LastWriteTime -ForegroundColor Cyan
Write-Warning "Setting write time from $longRemote onto $shortLocal"
$(Get-Item $shortLocal).LastWriteTime=$(Get-Item $longRemote).LastWriteTime
Write-Host "Time on shortLocal is now" $(Get-Item $shortLocal).LastWriteTime
}