Skip to content

Instantly share code, notes, and snippets.

@holmesw
Created June 11, 2024 18:21
Show Gist options
  • Save holmesw/eabd91de506cb3e183df5ea275de1b49 to your computer and use it in GitHub Desktop.
Save holmesw/eabd91de506cb3e183df5ea275de1b49 to your computer and use it in GitHub Desktop.
PowerShell Script to Generate a CSV Report of all Files in Directory (including subdirectories) That Were Created/Last Modified Over 6 Years Ago

PowerShell Script to Generate a CSV Report of Old Files

This PowerShell script generates a CSV report of all files in a specified directory (including subdirectories) that were created or last modified over 6 years ago. The report is saved to a specified location.

Script Description

Variables

  • $rootDirectory: Defines the root directory to search for files.
  • $outputCsv: Defines the output CSV file path where the report will be saved.
  • $currentDate: Stores the current date and time.
  • $thresholdDate: Calculates the threshold date, which is 6 years ago from the current date.

Main Script Logic

  1. Set Up Variables:

    • $rootDirectory: The directory to search for files.
    • $outputCsv: The path to save the generated CSV report.
    • $currentDate: Get the current date and time.
    • $thresholdDate: Calculate the date that is 6 years ago from the current date.
  2. Create Report Array:

    • An array $fileReport is initialized to store file information.
  3. Search and Filter Files:

    • Get-ChildItem is used to recursively find all files in the specified directory.
    • Each file is checked to see if its last modified date or creation date is older than the threshold date.
    • If a file meets the criteria, its information is added to the $fileReport array.
  4. Export to CSV:

    • The file information stored in $fileReport is exported to a CSV file using Export-Csv.
  5. Completion Message:

    • Outputs a message indicating that the CSV report has been generated and saved.

PowerShell Script

# Define the root directory to search for files
$rootDirectory = "C:\path\to\your\directory"

# Define the output CSV file path
$outputCsv = "C:\path\to\your\output\report.csv"

# Get the current date and time
$currentDate = Get-Date

# Calculate the threshold date (6 years ago from current date)
$thresholdDate = $currentDate.AddYears(-6)

# Create an array to store file information
$fileReport = @()

# 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) {
        # Add the file information to the report array
        $fileReport += [PSCustomObject]@{
            FilePath      = $_.FullName
            CreationTime  = $_.CreationTime
            LastWriteTime = $_.LastWriteTime
        }
    }
}

# Save the report to a CSV file
$fileReport | Export-Csv -Path $outputCsv -NoTypeInformation

# Indicate that the report generation is complete
Write-Host "CSV report generated and saved to $outputCsv"

Usage

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

  2. Set the Variables:

    • Modify the $rootDirectory variable to the path of the directory you want to process.
    • Modify the $outputCsv variable to the path where you want to save the CSV report.
  3. Run the Script: Open PowerShell with administrative privileges and navigate to the directory where you saved the script. Run the script by typing:

.\GenerateFileReport.ps1
  1. Script Output: The script will output a message indicating the path of the generated CSV report. The CSV file will contain the following columns for each file that meets the criteria:
    • FilePath: The full path of the file.
    • CreationTime: The creation date and time of the file.
    • LastWriteTime: The last modified date and time of the file.
# Define the root directory to search for files
$rootDirectory = "C:\path\to\your\directory"
# Define the output CSV file path
$outputCsv = "C:\path\to\your\output\report.csv"
# Get the current date and time
$currentDate = Get-Date
# Calculate the threshold date (6 years ago from current date)
$thresholdDate = $currentDate.AddYears(-6)
# Create an array to store file information
$fileReport = @()
# 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) {
# Add the file information to the report array
$fileReport += [PSCustomObject]@{
FilePath = $_.FullName
CreationTime = $_.CreationTime
LastWriteTime = $_.LastWriteTime
}
}
}
# Save the report to a CSV file
$fileReport | Export-Csv -Path $outputCsv -NoTypeInformation
# Indicate that the report generation is complete
Write-Host "CSV report generated and saved to $outputCsv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment