List old files on disk using PowerShell

Every company runs into this issue, after a while you have a hard drive full of files that no one wants to touch since they are not sure what is there and how long is been there. So backup job keeps growing. Here is a simple PowerShell script that will list files oldest to newest on your drive.

So in order to save some space on the backup drive, I came up with the following script to check for old files on the C drive:

get-childitem C:\ -rec | where {!$_.PSIsContainer} |select-object FullName, LastWriteTime, Length  | ft

we can further extend the script by showing only x oldest files like so:

I have chosen to show only the first 10 results you can change this to anything you like.

 get-childitem C:\ -rec | where {!$_.PSIsContainer} |select-object FullName, LastWriteTime, Length | Sort-Object -Property LastWriteTime |select -First 10 |ft

And here is the final version that exports to the CSV file for further processing:

 get-childitem C:\ -rec | where {!$_.PSIsContainer} |select-object FullName, LastWriteTime, Length | Sort-Object -Property LastWriteTime |Export-Csv -Path <somepath>\<exportfilename> -NoTypeInformation

Enjoy.