PowerShell – 7 Year File Audit


Data retention can be a real headache for system administrators. In my current role I am required to report on files in excess of 7 years since the last time they were written to, the below PowerShell script can be run against a directory or drive to report on these files and provide you with a total count for reporting purposes.

(Note : I typically run this script in conjunction with SysInternals PSExec to run the script at a system level and avoid any of the issues around permissions based access.)

$dir = "Drive Or Folder Path"
$years = "7"
Get-ChildItem -Path $dir -ErrorAction SilentlyContinue -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).addYears(-$years)} | select Name, Directory, Length, LastWriteTime | Export-Csv -Path "C:\FilesOver7Years.csv"
$NoOfFiles = Get-ChildItem -Path $dir -ErrorAction SilentlyContinue -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).addYears(-$years)}
($NoOfFiles).Count

If you wish you could write a report out to HTML and run this for management on an internal IIS instance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s