Facebook
From a, 5 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 137
  1. # Set the directory to scan
  2. $targetDirectory = "C:\MyDirectory"
  3.  
  4. # Set the output file path
  5. $outputFile = "C:\inactive_files.csv"
  6.  
  7. # Calculate the date six months ago
  8. $sixMonthsAgo = (Get-Date).AddMonths(-6)
  9.  
  10. # Get a list of files in the directory and its subdirectories
  11. $files = Get-ChildItem -Path $targetDirectory -Recurse -Force
  12.  
  13. # Filter the list to include only files older than 6 months
  14. $inactiveFiles = $files | Where-Object { $_.LastWriteTime -lt $sixMonthsAgo }
  15.  
  16. # Create a CSV object with the file name and last modified date
  17. $csvData = $inactiveFiles | Select-Object @{Name = "FileName"}, @{Name = "LastWriteTime"}, @{Name = "AgeInMonths", Expression = { Invoke-[removed](Get-Date).Subtract($_.LastWriteTime).TotalMonths) }}
  18.  
  19. # Export the CSV object to the output file
  20. $csvData | Export-Csv -Path $outputFile -NoTypeInformation