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.
$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.
-
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.
-
Create Report Array:
- An array
$fileReport
is initialized to store file information.
- An array
-
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.
-
Export to CSV:
- The file information stored in
$fileReport
is exported to a CSV file usingExport-Csv
.
- The file information stored in
-
Completion Message:
- Outputs a message indicating that the CSV report has been generated and saved.
# 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"
-
Save the Script: Save the above script to a file, for example,
GenerateFileReport.ps1
. -
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.
- Modify the
-
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
- 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.