# Set the directory to scan $targetDirectory = "C:\MyDirectory" # Set the output file path $outputFile = "C:\inactive_files.csv" # Calculate the date six months ago $sixMonthsAgo = (Get-Date).AddMonths(-6) # Get a list of files in the directory and its subdirectories $files = Get-ChildItem -Path $targetDirectory -Recurse -Force # Filter the list to include only files older than 6 months $inactiveFiles = $files | Where-Object { $_.LastWriteTime -lt $sixMonthsAgo } # Create a CSV object with the file name and last modified date $csvData = $inactiveFiles | Select-Object @{Name = "FileName"}, @{Name = "LastWriteTime"}, @{Name = "AgeInMonths", Expression = { Invoke-[removed](Get-Date).Subtract($_.LastWriteTime).TotalMonths) }} # Export the CSV object to the output file $csvData | Export-Csv -Path $outputFile -NoTypeInformation