3

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?

15
  • Answered here: How to modify Last Write Time and Last Access on folder
    – DavidPostill
    Commented Jun 11, 2022 at 15:54
  • You will want to do something like LastAccessTime -Value $($LastAccessTime) ... in order to achieve your goals.
    – Ramhound
    Commented Jun 11, 2022 at 15:58
  • @DavidPostill I forgot to mention that I tried Set-ItemProperty as well. It's a very weird case with that. If you check the value of $Item.LastAccessTime after Set-ItemProperty then you see the new set date, but in Windows Explorer and if you use $(Get-Item -Path "test.txt").LastAccessTime then you see the old access timestamp. I'll update the question with the weirdness.
    – Jim
    Commented Jun 11, 2022 at 16:17
  • @Jim - Are you refreshing the folder? When you modify a file, say opening a text file in notepad, the folder refreshes automatically. However, due to you not actually opening and modifying the file, you might have to refresh the folder. Now you likely can automatically perform this action with a PowerShell command.
    – Ramhound
    Commented Jun 11, 2022 at 16:54
  • 2
    @iTwasnTme hahaha, ok, mystery solved... I fired up Procmon and had a look at what was happening to the file. Sure enough, I see SearchProtocolHost.exe accessing the file after the timestamp was modified. I guess each time the timestamp gets updated, the SearchProtocolHost.exe comes along to re-index it and that updates the LastAccessTime. Re-running my test in an un-indexed directory yields a completely stable LastAccessTime and confirmed by Procmon showing no other processes touching the file. Thanks a lot for the help! (Thanks Vomit as well).
    – Jim
    Commented Jun 14, 2022 at 14:46

1 Answer 1

3

All 3 file timestamps (CreationTime, LastWriteTime, LastAccessTime) can be set with PowerShell.

The timestamps can either be set via the property, or by using the Set-ItemProperty cmdlet. Examples of valid ways to set any of the 3 timestamps:

$Item = New-Item "test.txt" -Force -Value "test"
# all equally valid ways to set timestamp:
$Item.CreationTime = "2000-01-01"
$(Get-Item "test.txt").LastWriteTime = "2000-01-01"
Set-ItemProperty -Path "test.txt" -Name LastAccessTime -Value "2000-01-01"
# see the result
Get-Item "test.txt" | Select CreationTime, LastWriteTime, LastAccessTime | Format-List

Note that setting the LastAccessTime can be unstable because other processes may read files when their timestamps are updated, and thus trigger the LastAccessTime to be updated. Possible culprits that update LastAccessTime include SearchProtocolHost.exe or anti-virus software.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .