This PowerShell script updates the creation and last modified dates of files in a specified directory (including subdirectories) that were created or modified over 6 years ago. The script sets the new creation date to 1 year ago and the new last modified date to 1 minute after the new creation date.
$rootDirectory
: Defines the root directory to search for files.$currentDate
: Stores the current date and time.$thresholdDate
: Calculates the threshold date, which is 6 years ago from the current date.$createdDate
: Calculates the new creation date, which is 1 year ago from the current date.$modifiedDate
: Calculates the new last modified date, which is 1 minute after the new creation date.
This function updates the creation and last modified dates of a file.
Parameters:
[string]$filePath
: Path of the file to update.[datetime]$createdDate
: New creation date.[datetime]$modifiedDate
: New last modified date.
Functionality:
- Sets the
LastWriteTime
(last modified time) to the new modified date. - Sets the
CreationTime
to the new created date. - Outputs the updated file path to the console.
- Outputs the root directory and threshold date to the console for reference.
- Uses
Get-ChildItem
to recursively find all files in the specified directory. - Checks if each file's last modified date or creation date is older than the threshold date.
- Calls the
Update-FileDates
function for files meeting the criteria. - Outputs "Update complete" to indicate the script has finished processing.
# Define the root directory to search for files
$rootDirectory = "C:\path\to\your\directory"
# Get the current date and time
$currentDate = Get-Date
# Calculate the threshold date (6 years ago from current date)
$thresholdDate = $currentDate.AddYears(-6)
# Calculate the new created date (1 year ago from current date)
$createdDate = $currentDate.AddYears(-1)
# Calculate the new last modified date (1 minute after the new created date)
$modifiedDate = $createdDate.AddMinutes(1)
# Function to update the creation and last modified dates of a file
function Update-FileDates {
param (
[string]$filePath, # Path of the file to update
[datetime]$createdDate, # New creation date
[datetime]$modifiedDate # New last modified date
)
# Set the LastWriteTime (last modified time) to the new modified date
Set-ItemProperty -Path $filePath -Name LastWriteTime -Value $modifiedDate
# Set the CreationTime to the new created date
Set-ItemProperty -Path $filePath -Name CreationTime -Value $createdDate
# Output the updated file path to the console
Write-Host "Updated $filePath"
}
# Output the root directory and threshold date to the console
Write-Host $rootDirectory
Write-Host $thresholdDate
# Get all files in the directory and subdirectories
Get-ChildItem -Path $rootDirectory -Recurse -File | ForEach-Object {
# Check if the file's last modified date or creation date is older than the threshold date
if ($_.LastWriteTime -lt $thresholdDate -or $_.CreationTime -lt $thresholdDate) {
# Update the file dates if the condition is met
Update-FileDates -filePath $_.FullName -createdDate $createdDate -modifiedDate $modifiedDate
}
}
# Indicate that the update process is complete
Write-Host "Update complete."
-
Save the Script: Save the above script to a file, for example,
UpdateFileDates.ps1
. -
Set the Root Directory: Modify the
$rootDirectory
variable to the path of the directory you want to process. -
Run the Script: Open PowerShell with administrative privileges and navigate to the directory where you saved the script. Run the script by typing:
.\UpdateFileDates.ps1
- Script Output: The script will output the root directory and the threshold date. It will then process each file, updating the dates as needed, and will output the path of each updated file. Finally, it will indicate that the update process is complete.