Skip to content

Instantly share code, notes, and snippets.

@holmesw
Created June 8, 2024 17:59
Show Gist options
  • Save holmesw/7029b60bd62c157d3de3032bd3e4c31e to your computer and use it in GitHub Desktop.
Save holmesw/7029b60bd62c157d3de3032bd3e4c31e to your computer and use it in GitHub Desktop.
PowerShell Script to Update File Date Properties

PowerShell Script to Update File Date Properties

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.

Script Description

Variables

  • $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.

Function

Update-FileDates

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.

Main Script Logic

  1. Outputs the root directory and threshold date to the console for reference.
  2. Uses Get-ChildItem to recursively find all files in the specified directory.
  3. Checks if each file's last modified date or creation date is older than the threshold date.
  4. Calls the Update-FileDates function for files meeting the criteria.
  5. Outputs "Update complete" to indicate the script has finished processing.

PowerShell Script

# 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."

Usage

  1. Save the Script: Save the above script to a file, for example, UpdateFileDates.ps1.

  2. Set the Root Directory: Modify the $rootDirectory variable to the path of the directory you want to process.

  3. 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
  1. 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.
# 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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment