0

Here is a script to remove file/folder older than X days. but then i thought what if any of file or folder is being using by other app. i haven't face such issue yet but i don't think Remove-Item cannot delete file/folder if it's locked/using by other app. is there anyway using powershell we can delete locked file?

Get-ChildItem "folder path" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-1))}| Remove-Item -Recurse -Force -Confirm:$false
Shahpari
  • 77
  • 5
  • Do not do this! The whole point of locking a file is to prevent access outside of the locking application. There are ways to forcibly close file handles, but that will cause issues on the application that holds the file open. – vonPryz Oct 10 '21 at 19:43
  • thank you for the suggestion. but it's okay in my case. – Shahpari Oct 10 '21 at 19:46
  • See [this answer](https://stackoverflow.com/a/958580/45375) for how to find the processes that have a given file locked, but note that that it requires installation of the `handle.exe` SysInternals utility. – mklement0 Oct 10 '21 at 20:33
  • I am pretty sure you are working on an XY problem. A good solution is to notify the lock-holders to close the files. Or terminate the application that holds the lock, if it is stuck. – vonPryz Oct 11 '21 at 07:11

1 Answers1

-2
Remove-Item

will delete the locked files as well. Please validate