I tried Unlocker 1.9.1 but it doesn't work correctly for me on Windows7 (worked ok on Windows XP) and also I tried LockHunter x64 and reported a bug but .... LockHunter actually unlocks the file from GUI but not from command line.

So I want to use handle.exe by SysInternals to unlock one file "TestPro.log". I know the absolut path if it helps. I can list and all processes that locked the file by executing

C:\Windows\system32>c:\edutester\progs\handle testpro.log
java.exe           pid: 2120   type: File           338: C:\Users\Public\TestPro
\TestPro Automation Framework\Logs\TestPro.log
java.exe           pid: 1004   type: File           934: C:\Users\Public\TestPro
\TestPro Automation Framework\Logs\TestPro.log

What I need to know how to unlock the file using above info from command line automatically. No user intervention is possible.

  • Windows 7 64bit
  • Microsoft Windows [Version 6.1.7601]

You can use handle.exe once to list the PIDs and handles, then again multiple times to close each one.

Use the for /f command to loop through the result of the first command. I don't have a Windows machine handy to test on, but it should look something like this:

From a batch file:

for /F "tokens=3,6 delims=: " %%I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %%J -y -p %%I

From the command-line:

for /F "tokens=3,6 delims=: " %I IN ('handle.exe -accepteula TestPro.log') DO handle.exe -c %J -y -p %I
| improve this answer | |
  • I cannot make it work. Your code gives me ('handle.exe was unexpected at this time. I think there is missing in before ('handle.exe was unexpected at this time. But then %%J contains :` so it's not valid argument for handle.exe – Radek Oct 12 '12 at 5:04
  • Yeah, I remember having trouble getting the command exactly right, but I can't remember what I did off the top of my head. I'm trying to get access to my batch file again so I can update my answer with the right syntax. – Stephen Jennings Oct 12 '12 at 5:23
  • @Radek I updated my answer with the actual code I used. Looks like I got %%I and %%J mixed up, and we need the colon as a delimiter. Try this and see how it goes. – Stephen Jennings Oct 12 '12 at 15:08
  • Works nicely (both batch file & command line versions) if cmd started as administrator. Any idea how I can make it work as a part of my overnight automation testing? runas doesn't work for me in this case. – Radek Oct 14 '12 at 22:59
  • Got it working. The trick is that the command to run under runas is in fact cmd.exe and the argument for cmd is your code. I was doing it without cmd. – Radek Oct 14 '12 at 23:25



for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do @echo %i %j                                                                    


for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i  


for /f "tokens=3,6 skip=5 delims=: " %i in ('handle.exe -accepteula notepad.exe') do handle.exe -c %j -y -p %i & taskkill /t /f /PID %i                                                       
| improve this answer | |
  • 1
    Can you expand on this and explain how it improves on the (four and a half year old) accepted answer? – bertieb Feb 16 '17 at 10:48
  • I see your commands are different than the accepted answer, so its a legitimate answer. However, as bertieb indicated, your answer needs an explanation as to what your commands will do and how they address the OPs question. Thanks for contributing to SuperUser. – I say Reinstate Monica Feb 16 '17 at 22:37

Execution from the context menu without a bat file. 1) Put the file handle64.exe in C:\Windows. 2) Create and execute reg script

    Windows Registry Editor Version 5.00

    "MUIVerb"="Что блокирует?"

    @="C:\\Windows\\system32\\Cmd.exe /c C:\\Windows\\handle64.exe -u \"%1\" & pause"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.