I am using PowerShell to create test files for testing the output of Robocopy logs. To get different combinations of newer and older files, I am setting the file timestamps in the source and destination. However, I am unable to control the LastAccessTime attribute, which is preventing me from creating a file that Robocopy sees as completely identical ("same").
I can set CreationTime and LastWriteTime to any timestamp. But setting the LastAccessTime only refreshes the LastAccessTime to the current date and time.
$Item = New-Item "test.txt" -Force -Value "test"
$Item.CreationTime = "2000-01-01"
$Item.LastWriteTime = "2000-01-01"
$Item.LastAccessTime= "2000-01-01" # doesn't work
Set-ItemProperty -Path "test.txt" -Name LastAccessTime -Value "2000-01-01" # also doesn't work
$Item = $(Get-Item -Path "test.txt") # refresh the $Item otherwise we see a phantom update of LastAccessTime
Write-Host "$($Item.CreationTime) $($Item.LastWriteTime) $($Item.LastAccessTime))"
I expected the output to be:
01/01/2000 00:00:00 01/01/2000 00:00:00 01/01/2000 00:00:00
But the actual output was:
01/01/2000 00:00:00 01/01/2000 00:00:00 06/11/2022 17:29:35
If I wait a few seconds and then re-run the last three lines, the LastAccessTime will be changed to the current date and time again.
Is there a way to set the LastAccessTime with PowerShell?
LastAccessTime -Value $($LastAccessTime) ...
in order to achieve your goals.