Remove older files by last access flag

EdWin 121 Reputation points

Hi everyone,

I need a script (or comand, whatever) that remove old files from a File Server. But, I have to delete these files by "last accessed files" from before a specific date (before 01/01/2019 for example). I've used forfiles command to achieve this task, but forfiles exclude files just by their last modified date. The picture below shows the dates of a file:


I'd like to know whether it's possible to remove files by last accessed date or not. If so, which tool can I use for this?

Thank you

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,737 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 27,831 Reputation points Microsoft Vendor


    You could do it with powershell script like this

    [System.DateTime]$date="10/22/2020 16:10:23"  
    $YourPath = "X:\YourPath"  
    Get-ChildItem -Path $YourPath -Recurse | Where-Object {$_.LastAccessTime -lt $date} | Remove-Item  

    This will remove all the files last accessed before 10/22/2020 16:10:23 in X:\YourPath including files in subfolders.

    Best Regards,


    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments