I am trying to change the "Date Modified" (LastWriteTime) of over 2K files in a specific folder. All files were modified on the same Day. However, it does have multiple times. So, basically what I am trying to accomplish is this:
Myfile1.Zip --> Date Modified = "1/4/2022 12:21 PM" ==> Date Modified = "1/3/2022 9:00 AM"
Myfile2.Zip --> Date Modified = "1/4/2022 12:25 PM" ==> Date Modified = "1/3/2022 9:00 AM"
Meaning that all files that were Modified on 1/4/2021 needs to display "Date Modified" as 1/3/2021, time really does not matter here.
Is it possible to do a bulk change with a PowerShell Script? I am not familiar at all with it and need this change made asap.
Thanks in Advance for your help.
CodePudding user response:
You can inspect the existing LastWriteTime property on file system objects returned by Get-ChildItem, then assign a new value to the same:
# define date/time variables
$filterDate = (Get-Date -Day 4 -Month 1 -Year 2022).Date
$targetDateTime = Get-Date -Day 3 -Month 1 -Year 2022 -Hour 9 -Minute 0 -Second 0
# locate and filter relevant files
$relevantFiles = Get-ChildItem -Path .\path\to\folder\ |Where-Object { $_.LastWriteTime.Date -eq $filterDate }
# update their timestamps
$relevantFiles |ForEach-Object {
$_.LastWriteTime = $targetDateTime
}
Dereferencing the Date property on an existing [DateTime] value gives you the date at midnight, so the comparison $_.LastWriteTime.Date -eq $filterDate will work regardless of whether the file was updated at 1AM or 6PM, as long as it was on January 4.
