Using in PowerShell, how can I check if an application is locking a file?
I like to check which process/application is using the file, so that I can close it.
Using in PowerShell, how can I check if an application is locking a file?
I like to check which process/application is using the file, so that I can close it.
You can do this with the SysInternals tool handle.exe. Try something like this:
You can find a solution using Sysinternal's Handle utility.
I had to modify the code (slightly) to work with PowerShell 2.0:
Example:
I've seen a nice solution at Locked file detection that uses only PowerShell and .NET framework classes:
If you modify the above function slightly like below it will return True or False (you will need to execute with full admin rights) e.g. Usage:
You should be able to use the openfiles command from either the regular command line or from PowerShell.
The openfiles built-in tool can be used for file shares or for local files. For local files, you must turn on the tool and restart the machine (again, just for first time use). I believe the command to turn this feature on is:
For example (works on Windows Vista x64):
That successfully returns file handles associated with Chrome. You can also pass in a file name to see the process currently accessing that file.
This could help you: Use PowerShell to find out which process locks a file. It parses the System.Diagnostics.ProcessModuleCollection Modules property of each process and it looks for the file path of the locked file: