How to list directories that haven’t been updated in X amount of time. To find this we’re going to use Get-ChildItem and the LastWriteTime property. Here is what the mini script looks like.
List Directories That Haven’t Been Updated
#Enter in the path to search for. #Using the 'Recurse' switch will search through all sub-directories as well. $Path = "\\PAC-FS01\Apps" #Searching for directories that haven't been updated in 6 Months. Get-ChildItem -Path $Path -Recurse -Directory | Where-Object {($_.LastWriteTime -lt (Get-Date).AddMonths(-6) )} | select FullName, LastWriteTime | Export-Csv C:\Oldfiles.csv -NoTypeInformation
Normally when a file is modified the parent directory will also show modified. Here in my example I haven’t modified any files in my 2012 SCCM Setup Files.
Note: Just because the file has not been modified doesn’t mean it hasn’t been accessed.
Hopefully this little quick tip was enough to get you started. From here you can easily move the files and directories using the Robocopy cmdlet with the switches in the MS post. Or if you’re feeling really brave, you can pipe the results to a Remove-Item to delete all those old files, however I would be very cautious before doing this and generally not recommend it.
As always, don’t forget to subscribe to our YouTube Channel to get up to date sysadmin content.